JavaScript - 获取两个日期之间的分钟数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7709803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript - Get minutes between two dates
提问by John
If I have two dates, how can I use JavaScriptto get the difference between the two dates in minutes?
如果我有两个日期,我如何使用JavaScript在几分钟内获得两个日期之间的差异?
回答by Darin Dimitrov
You may checkout this code:
您可以查看此代码:
var today = new Date();
var Christmas = new Date("2012-12-25");
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 =)");
or var diffMins = Math.floor((...
to discard seconds if you don't want to round minutes.
或者var diffMins = Math.floor((...
如果您不想舍入分钟,则丢弃秒数。
回答by KooiInc
Subtracting 2 Date objects gives you the difference in milliseconds, e.g.:
减去 2 个 Date 对象会给你以毫秒为单位的差异,例如:
var diff = Math.abs(new Date('2011/10/09 12:00') - new Date('2011/10/09 00:00'));
Math.abs
is used to be able to use the absolute difference (so new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00')
gives the same result).
Math.abs
用于能够使用绝对差异(因此new Date('2011/10/09 00:00') - new Date('2011/10/09 12:00')
给出相同的结果)。
Dividing the result by 1000 gives you the number of seconds. Dividing that by 60 gives you the number of minutes. To round to whole minutes, use Math.floor
or Math.ceil
:
结果除以 1000 就是秒数。将其除以 60 就是分钟数。要四舍五入到整分钟,请使用Math.floor
或Math.ceil
:
var minutes = Math.floor((diff/1000)/60);
In this example the result will be 720
在这个例子中,结果将是 720
回答by MrGorki
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);
回答by AlliterativeAlice
A simple function to perform this calculation:
执行此计算的简单函数:
function getMinutesBetweenDates(startDate, endDate) {
var diff = endDate.getTime() - startDate.getTime();
return (diff / 60000);
}
回答by Purkhalo Alex
That's should show the difference between the two dates in minutes. Try it in your browser:
这应该以分钟为单位显示两个日期之间的差异。在浏览器中尝试:
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
回答by Rick
For those that like to work with small numbers
对于那些喜欢处理小数字的人
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);
回答by Marco Blos
This problem is solved easily with moment.js, like this example:
这个问题用 moment.js 很容易解决,就像这个例子:
var difference = mostDate.diff(minorDate, "minutes");
var difference = mostDate.diff(minorDate, "minutes");
The second parameter can be changed for another parameters, see the moment.js documentation.
第二个参数可以更改为另一个参数,请参阅 moment.js 文档。
e.g.: "days", "hours", "minutes", etc.
例如:“天”、“小时”、“分钟”等。
The CDN for moment.js is available here:
moment.js 的 CDN 可在此处获得:
https://cdnjs.com/libraries/moment.js
https://cdnjs.com/libraries/moment.js
Thanks.
谢谢。
EDIT:
编辑:
mostDate and minorDate should be a moment type.
mostDate 和 minorDate 应该是一个时刻类型。
回答by Gio Palacino
Here's some fun I had solving something similar in node.
这是我在 node.js 中解决类似问题的一些乐趣。
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
*/
You can see it in action at https://repl.it/@GioCirque/TimeSpan-Formatting
您可以在https://repl.it/@GioCirque/TimeSpan-Formatting 上看到它的实际效果