我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?


当前回答

这段代码将返回两个日期的差值(以天为单位):

const previous_date = new Date("2019-12-23");
const current_date = new Date();

const current_year = current_date.getFullYear();
const previous_date_year = 
previous_date.getFullYear();

const difference_in_years = current_year - 
previous_date_year;

let months = current_date.getMonth();
months = months + 1; // for making the indexing 
// of months from 1

for(let i = 0; i < difference_in_years; i++){
months = months + 12;
}

let days = current_date.getDate();

days = days + (months * 30.417);

console.log(`The days between ${current_date} and 
${previous_date} are : ${days} (approximately)`);

其他回答

如果你使用的是moment.js,那么查找日期差异就很简单了。

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

function daysInMonth (month, year) { return new Date(year, month, 0).getDate(); } function getduration(){ let A= document.getElementById("date1_id").value let B= document.getElementById("date2_id").value let C=Number(A.substring(3,5)) let D=Number(B.substring(3,5)) let dif=D-C let arr=[]; let sum=0; for (let i=0;i<dif+1;i++){ sum+=Number(daysInMonth(i+C,2019)) } let sum_alter=0; for (let i=0;i<dif;i++){ sum_alter+=Number(daysInMonth(i+C,2019)) } let no_of_month=(Number(B.substring(3,5)) - Number(A.substring(3,5))) let days=[]; if ((Number(B.substring(3,5)) - Number(A.substring(3,5)))>0||Number(B.substring(0,2)) - Number(A.substring(0,2))<0){ days=Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter } if ((Number(B.substring(3,5)) == Number(A.substring(3,5)))){ console.log(Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter) } time_1=[]; time_2=[]; let hour=[]; time_1=document.getElementById("time1_id").value time_2=document.getElementById("time2_id").value if (time_1.substring(0,2)=="12"){ time_1="00:00:00 PM" } if (time_1.substring(9,11)==time_2.substring(9,11)){ hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2))) } if (time_1.substring(9,11)!=time_2.substring(9,11)){ hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2)))+12 } let min=Math.abs(Number(time_1.substring(3,5))-Number(time_2.substring(3,5))) document.getElementById("duration_id").value=days +" days "+ hour+" hour " + min+" min " } <input type="text" id="date1_id" placeholder="28/05/2019"> <input type="text" id="date2_id" placeholder="29/06/2019"> <br><br> <input type="text" id="time1_id" placeholder="08:01:00 AM"> <input type="text" id="time2_id" placeholder="00:00:00 PM"> <br><br> <button class="text" onClick="getduration()">Submit </button> <br><br> <input type="text" id="duration_id" placeholder="days hour min">

我想这个就可以了。

let today = new Date();
let form_date=new Date('2019-10-23')
let difference=form_date>today ? form_date-today : today-form_date
let diff_days=Math.floor(difference/(1000*3600*24))

可以是有用的:

const date_diff = (date1, date2) => Math.ceil(Math.abs(date1 - date2)/24 * 60 * 60 * 1000)

or

const date_diff = (date1, date2) => Math.ceil(Math.abs(date1 - date2)/86400000)

24 * 60 * 60 * 1000在一天内(天*分*秒*毫秒)= 86400000毫秒

谢谢你!

像“差几天”这样的表达从来不像看起来那么简单。如果你有以下日期:

d1: 2011-10-15 23:59:00
d1: 2011-10-16 00:01:00

时间差2分钟,“天数差”应该是1还是0?类似的问题也出现在任何以月、年或其他形式表示的差异中,因为年、月和日的长度和时间不同(例如,夏令时开始的那一天比平时短1小时,比夏令时结束的那一天短2小时)。

这里是一个忽略时间的天数差函数,即对于上述日期,它返回1。

/*
   Get the number of days between two dates - not inclusive.

   "between" does not include the start date, so days
   between Thursday and Friday is one, Thursday to Saturday
   is two, and so on. Between Friday and the following Friday is 7.

   e.g. getDaysBetweenDates( 22-Jul-2011, 29-jul-2011) => 7.

   If want inclusive dates (e.g. leave from 1/1/2011 to 30/1/2011),
   use date prior to start date (i.e. 31/12/2010 to 30/1/2011).

   Only calculates whole days.

   Assumes d0 <= d1
*/
function getDaysBetweenDates(d0, d1) {

  var msPerDay = 8.64e7;

  // Copy dates so don't mess them up
  var x0 = new Date(d0);
  var x1 = new Date(d1);

  // Set to noon - avoid DST errors
  x0.setHours(12,0,0);
  x1.setHours(12,0,0);

  // Round to remove daylight saving errors
  return Math.round( (x1 - x0) / msPerDay );
}

这可以更简洁:

/* Return number of days between d0 and d1. ** Returns positive if d0 < d1, otherwise negative. ** ** e.g. between 2000-02-28 and 2001-02-28 there are 366 days ** between 2015-12-28 and 2015-12-29 there is 1 day ** between 2015-12-28 23:59:59 and 2015-12-29 00:00:01 there is 1 day ** between 2015-12-28 00:00:01 and 2015-12-28 23:59:59 there are 0 days ** ** @param {Date} d0 - start date ** @param {Date} d1 - end date ** @returns {number} - whole number of days between d0 and d1 ** */ function daysDifference(d0, d1) { var diff = new Date(+d1).setHours(12) - new Date(+d0).setHours(12); return Math.round(diff/8.64e7); } // Simple formatter function formatDate(date){ return [date.getFullYear(),('0'+(date.getMonth()+1)).slice(-2),('0'+date.getDate()).slice(-2)].join('-'); } // Examples [[new Date(2000,1,28), new Date(2001,1,28)], // Leap year [new Date(2001,1,28), new Date(2002,1,28)], // Not leap year [new Date(2017,0,1), new Date(2017,1,1)] ].forEach(function(dates) { document.write('From ' + formatDate(dates[0]) + ' to ' + formatDate(dates[1]) + ' is ' + daysDifference(dates[0],dates[1]) + ' days<br>'); });