用分钟计算两次约会的差异

Get the Difference Between Two Dates in Minutes

本文关键字:约会 两次 分钟 计算      更新时间:2023-09-26

我有两个日期startdatecurrentdate

如何获得两个日期之间的差异(以分钟为单位)

console.log("startdate: " + startdate + " currentdate: " + currentdate);
>> startdate: Tue Nov 10 2015 13:00:00 GMT-0800 (PST) 
>> currentdate: Tue Nov 10 2015 11:55:53 GMT-0800 (PST)
console.log(startdate.getTime() - currentdate.getTime());
>> 3846736

我可以减去这两个日期,但我不确定如何将输出(output: 3846736)转换为分钟。

转换单位:

var milliseconds = (startdate.getTime() - currentDate.getTime());
var seconds = milliseconds / 1000;
var minutes = seconds / 60;

或者,一体化:

var minutes = (startdate.getTime() - currentDate.getTime()) / 1000 / 60;

这很可能会给你一个大的小数。对于舍入,请使用Math.floorMath.ceilMath.round