我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?
当前回答
另一种解决方案是将difference转换为一个新的Date对象,并获得该日期的年(与1970年不同)、月、日等。
var date1 = new Date(2010, 6, 17);
var date2 = new Date(2013, 12, 18);
var diff = new Date(date2.getTime() - date1.getTime());
// diff is: Thu Jul 05 1973 04:00:00 GMT+0300 (EEST)
console.log(diff.getUTCFullYear() - 1970); // Gives difference as year
// 3
console.log(diff.getUTCMonth()); // Gives month count of difference
// 6
console.log(diff.getUTCDate() - 1); // Gives day count of difference
// 4
所以差异就像“3年6个月零4天”。如果你想以一种人类可读的风格呈现不同,这将对你有所帮助。
其他回答
this should work just fine if you just need to show what time left, since JavaScript uses frames for its time you'll have get your End Time - The Time RN after that we can divide it by 1000 since apparently 1000 frames = 1 seconds, after that you can use the basic math of time, but there's still a problem to this code, since the calculation is static, it can't compensate for the different day total in a year (360/365/366), the bunch of IF after the calculation is to make it null if the time is lower than 0, hope this helps even though it's not exactly what you're asking :)
var now = new Date();
var end = new Date("End Time");
var total = (end - now) ;
var totalD = Math.abs(Math.floor(total/1000));
var years = Math.floor(totalD / (365*60*60*24));
var months = Math.floor((totalD - years*365*60*60*24) / (30*60*60*24));
var days = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24)/ (60*60*24));
var hours = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24)/ (60*60));
var minutes = Math.floor((totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24 - hours*60*60)/ (60));
var seconds = Math.floor(totalD - years*365*60*60*24 - months*30*60*60*24 - days*60*60*24 - hours*60*60 - minutes*60);
var Y = years < 1 ? "" : years + " Years ";
var M = months < 1 ? "" : months + " Months ";
var D = days < 1 ? "" : days + " Days ";
var H = hours < 1 ? "" : hours + " Hours ";
var I = minutes < 1 ? "" : minutes + " Minutes ";
var S = seconds < 1 ? "" : seconds + " Seconds ";
var A = years == 0 && months == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0 ? "Sending" : " Remaining";
document.getElementById('txt').innerHTML = Y + M + D + H + I + S + A;
可以是有用的:
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毫秒
谢谢你!
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">
如果你使用的是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")
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));
代码样本从这里。