如果我有两个日期,我如何使用JavaScript以分钟为单位来区分这两个日期?
答案 0 :(得分:169)
您可以查看此代码:
var today = new Date();
var Christmas = new Date("12-25-2012");
var diffMs = (Christmas - today); // milliseconds between now & Christmas
var diffDays = Math.floor(diffMs / 86400000); // days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
alert(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas 2009 =)");
或var diffMins = Math.floor((...
如果您不想在几分钟内放弃秒数。
答案 1 :(得分:136)
减去2个Date对象可以得到毫秒数的差异,例如:
var diff = Math.abs(new Date('2011/10/09 12:00') - new Date('2011/10/09 00:00'));
Math.abs
用于能够使用绝对差值(因此new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00')
给出相同的结果)。
将结果除以1000可得出秒数。将它除以60可得出分钟数。要舍入到整分钟,请使用Math.floor
或Math.ceil
:
var minutes = Math.floor((diff/1000)/60);
在此示例中,结果将为720
答案 2 :(得分:42)
var startTime = new Date('2012/10/09 12:00');
var endTime = new Date('2013/10/09 12:00');
var difference = endTime.getTime() - startTime.getTime(); // This will give difference in milliseconds
var resultInMinutes = Math.round(difference / 60000);
答案 3 :(得分:38)
执行此计算的简单函数:
function getMinutesBetweenDates(startDate, endDate) {
var diff = endDate.getTime() - startDate.getTime();
return (diff / 60000);
}
答案 4 :(得分:9)
使用moment.js可以轻松解决此问题,例如:
var difference = mostDate.diff(minorDate, "minutes");
可以为其他参数更改第二个参数,请参阅moment.js文档。
例如:"天","小时","分钟"等
CDN for moment.js可在此处获取:
https://cdnjs.com/libraries/moment.js
感谢。
编辑:
mostDate和minorDate应该是片刻类型。
答案 5 :(得分:9)
对于那些喜欢使用小号的人
const today = new Date();
const endDate = new Date(startDate.setDate(startDate.getDate() + 7));
const days = parseInt((endDate - today) / (1000 * 60 * 60 * 24));
const hours = parseInt(Math.abs(endDate - today) / (1000 * 60 * 60) % 24);
const minutes = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000 * 60) % 60);
const seconds = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000) % 60);
答案 6 :(得分:9)
这应该以分钟为单位显示两个日期之间的差异。
const currDate = new Date('Tue Feb 13 2018 13:04:58 GMT+0200 (EET)')
const oldDate = new Date('Tue Feb 13 2018 12:00:58 GMT+0200 (EET)')
(currDate - oldDate) / 60000 # 64.0026
答案 7 :(得分:2)
您可以执行以下操作:
difference of dates
(差异将以毫秒为单位)milliseconds
转换为minutes
,即ms/1000/60
代码:
let dateOne = new Date("2020-07-10");
let dateTwo = new Date("2020-07-11");
let msDifference = dateTwo - dateOne;
let minutes = Math.floor(msDifference/1000/60);
console.log("Minutes between two dates =",minutes);
答案 8 :(得分:0)
我在节点上解决了类似的问题,这很有趣。
function formatTimeDiff(date1, date2) {
return Array(3)
.fill([3600, date1.getTime() - date2.getTime()])
.map((v, i, a) => {
a[i+1] = [a[i][0]/60, ((v[1] / (v[0] * 1000)) % 1) * (v[0] * 1000)];
return `0${Math.floor(v[1] / (v[0] * 1000))}`.slice(-2);
}).join(':');
}
const millis = 1000;
const utcEnd = new Date(1541424202 * millis);
const utcStart = new Date(1541389579 * millis);
const utcDiff = formatTimeDiff(utcEnd, utcStart);
console.log(`Dates:
Start : ${utcStart}
Stop : ${utcEnd}
Elapsed : ${utcDiff}
`);
/*
Outputs:
Dates:
Start : Mon Nov 05 2018 03:46:19 GMT+0000 (UTC)
Stop : Mon Nov 05 2018 13:23:22 GMT+0000 (UTC)
Elapsed : 09:37:02
*/
您可以在https://repl.it/@GioCirque/TimeSpan-Formatting上看到它的运行情况
答案 9 :(得分:0)
以下代码对我有用,
function timeDiffCalc(dateNow,dateFuture) {
var newYear1 = new Date(dateNow);
var newYear2 = new Date(dateFuture);
var dif = (newYear2 - newYear1);
var dif = Math.round((dif/1000)/60);
console.log(dif);
}