小编典典

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

all

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


阅读 62

收藏
2022-06-18

共1个答案

小编典典

您可以签出此代码:

var today = new Date();
var Christmas = new Date(today.getFullYear() + "-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
console.log(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas =)");

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

2022-06-18