我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?
当前回答
function DateDiff(b, e)
{
let
endYear = e.getFullYear(),
endMonth = e.getMonth(),
years = endYear - b.getFullYear(),
months = endMonth - b.getMonth(),
days = e.getDate() - b.getDate();
if (months < 0)
{
years--;
months += 12;
}
if (days < 0)
{
months--;
days += new Date(endYear, endMonth, 0).getDate();
}
return [years, months, days];
}
[years, months, days] = DateDiff(
new Date("October 21, 1980"),
new Date("July 11, 2017")); // 36 8 20
其他回答
像“差几天”这样的表达从来不像看起来那么简单。如果你有以下日期:
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>'); });
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">
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return Math.floor((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var dString = "May, 20, 1984";
var d1 = new Date(dString);
var d2 = new Date();
document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));
代码样本从这里。
加上@paresh mayani的答案,像Facebook一样工作-显示了以秒/分钟/小时/周/月/年为单位流逝了多少时间
var DateDiff = {
inSeconds: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/1000);
},
inMinutes: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/60000);
},
inHours: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/3600000);
},
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var dString = "May, 20, 1984"; //will also get (Y-m-d H:i:s)
var d1 = new Date(dString);
var d2 = new Date();
var timeLaps = DateDiff.inSeconds(d1, d2);
var dateOutput = "";
if (timeLaps<60)
{
dateOutput = timeLaps+" seconds";
}
else
{
timeLaps = DateDiff.inMinutes(d1, d2);
if (timeLaps<60)
{
dateOutput = timeLaps+" minutes";
}
else
{
timeLaps = DateDiff.inHours(d1, d2);
if (timeLaps<24)
{
dateOutput = timeLaps+" hours";
}
else
{
timeLaps = DateDiff.inDays(d1, d2);
if (timeLaps<7)
{
dateOutput = timeLaps+" days";
}
else
{
timeLaps = DateDiff.inWeeks(d1, d2);
if (timeLaps<4)
{
dateOutput = timeLaps+" weeks";
}
else
{
timeLaps = DateDiff.inMonths(d1, d2);
if (timeLaps<12)
{
dateOutput = timeLaps+" months";
}
else
{
timeLaps = DateDiff.inYears(d1, d2);
dateOutput = timeLaps+" years";
}
}
}
}
}
}
alert (dateOutput);
我做了一个下面的函数来得到现在和“2021-02-26T21:50:42.123”之间的区别。
差值返回以毫秒为单位的答案,所以我使用以下公式进行转换:
(1000 * 3600 * 24).
function getDiff(dateAcquired) {
let calDiff = Math.floor(
(new Date() - new Date(dateAcquired)) / (1000 * 3600 * 24)
);
return calDiff;
}
console.log(getDiff("2021-02-26T21:50:42.123"));