JavaScript - 获取两个日期之间的分钟数

时间:2022-08-03 21:30:59

If I have two dates, how can I use JavaScript to get the difference between the two dates in minutes?

如果我有两个日期,我如何使用JavaScript在几分钟内获得两个日期之间的差异?

7 个解决方案

#1


146  

You may checkout this code:

你可以签出这个代码:

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 =)");

or var diffMins = Math.floor((... to discard seconds if you don't want to round minutes.

或者var diffMins = Math.floor((...如果你不想圆几分钟就丢弃秒。

#2


102  

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用于能够使用绝对差异(因此新日期('2011/10/09 00:00') - 新日期('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

#3


27  

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);

#4


21  

A simple function to perform this calculation:

执行此计算的简单函数:

function getMinutesBetweenDates(startDate, endDate) {
    var diff = endDate.getTime() - startDate.getTime();
    return (diff / 60000);
}

#5


6  

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.

例如:“天”,“小时”,“分钟”等。

http://momentjs.com/docs/

http://momentjs.com/docs/

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应该是片刻类型。

#6


4  

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); 

#7


3  

That's should show the difference between the two dates in minutes.

这应该以分钟为单位显示两个日期之间的差异。

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

#1


146  

You may checkout this code:

你可以签出这个代码:

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 =)");

or var diffMins = Math.floor((... to discard seconds if you don't want to round minutes.

或者var diffMins = Math.floor((...如果你不想圆几分钟就丢弃秒。

#2


102  

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用于能够使用绝对差异(因此新日期('2011/10/09 00:00') - 新日期('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

#3


27  

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);

#4


21  

A simple function to perform this calculation:

执行此计算的简单函数:

function getMinutesBetweenDates(startDate, endDate) {
    var diff = endDate.getTime() - startDate.getTime();
    return (diff / 60000);
}

#5


6  

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.

例如:“天”,“小时”,“分钟”等。

http://momentjs.com/docs/

http://momentjs.com/docs/

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应该是片刻类型。

#6


4  

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); 

#7


3  

That's should show the difference between the two dates in minutes.

这应该以分钟为单位显示两个日期之间的差异。

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