如果我有两个日期,如何使用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((…如果不想舍入分钟,则丢弃秒。

其他回答

这是可行的

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

你可以签出这段代码:

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((…如果不想舍入分钟,则丢弃秒。

您可以进行如下操作:

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

它很容易工作:

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