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


你可以签出这段代码:

var today = new Date(); var Christmas = new Date(today.getFullYear() + “-12-25”); var diffMs = (圣诞节 - 今天);从现在到圣诞节之间的毫秒数 var diffDays = Math.floor(diffMs / 86400000);日 var diffHrs = Math.floor((diffMs % 86400000) / 3600000);小时 var diffMins = Math.round((((diffMs x86400000) % 3600000) / 60000);纪要 console.log(diffDays + “ days, ” + diffHrs + “ hours, ” + diffMins + “ minutes till Christmas =)”);

或者var diffMins = Math.floor((…如果不想舍入分钟,则丢弃秒。


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

一个简单的函数来执行这个计算:

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

这个问题用moment.js很容易解决,就像下面这个例子:

var差异= mostDate。鼩(minorDate,“分钟”);

第二个参数可以更改为其他参数,请参阅moment.js文档。

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

http://momentjs.com/docs/

moment.js的CDN在这里:

https://cdnjs.com/libraries/moment.js

谢谢。

编辑:

mostDate和minorDate应该是moment类型。

编辑2:

对于那些在2020年以后阅读我的答案的人来说,momentjs现在是一个遗留项目。

如果你还在寻找一个知名的库来做这项工作,我会推荐date-fns。

// How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
var result = differenceInMinutes(
  new Date(2014, 6, 2, 12, 20, 0),
  new Date(2014, 6, 2, 12, 7, 59)
)
//=> 12

对于那些喜欢处理小数字的人

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

这应该以分钟为单位显示两个日期之间的差异。在浏览器中试试:

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

这是我在节点中求解类似问题时得到的一些乐趣。

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上看到它的运行


下面的代码对我有用,

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

}

您可以进行如下操作:

获取日期的差异(以毫秒为单位) 将毫秒转换为分钟i-e 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);

这是可行的

duration = moment.duration(moment(end_time).diff(moment(start_time)))

它很容易工作:

var endTime = $("#ExamEndTime").val();
var startTime = $("#ExamStartTime").val();

//create date format          
var timeStart = new Date("01/01/2007 " + startTime);
var timeEnd = new Date("01/01/2007 " + endTime);
                
var  msInMinute = 60 * 1000;

var difference =  Math.round(Math.abs(timeEnd - timeStart) / msInMinute);

$("#txtCalculate").val(difference);