如果我有两个日期,我如何使用JavaScript在几分钟内获得两个日期之间的差异?
您可以签出此代码:
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((...如果您不想舍入分钟,则丢弃秒。
var diffMins = Math.floor((...