例如,在输入框中给定两个日期:
<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>
<script>
alert(datediff("day", first, second)); // what goes here?
</script>
如何在JavaScript中获得两个日期之间的天数?
例如,在输入框中给定两个日期:
<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>
<script>
alert(datediff("day", first, second)); // what goes here?
</script>
如何在JavaScript中获得两个日期之间的天数?
当前回答
function formatDate(seconds, dictionary) {
var foo = new Date;
var unixtime_ms = foo.getTime();
var unixtime = parseInt(unixtime_ms / 1000);
var diff = unixtime - seconds;
var display_date;
if (diff <= 0) {
display_date = dictionary.now;
} else if (diff < 60) {
if (diff == 1) {
display_date = diff + ' ' + dictionary.second;
} else {
display_date = diff + ' ' + dictionary.seconds;
}
} else if (diff < 3540) {
diff = Math.round(diff / 60);
if (diff == 1) {
display_date = diff + ' ' + dictionary.minute;
} else {
display_date = diff + ' ' + dictionary.minutes;
}
} else if (diff < 82800) {
diff = Math.round(diff / 3600);
if (diff == 1) {
display_date = diff + ' ' + dictionary.hour;
} else {
display_date = diff + ' ' + dictionary.hours;
}
} else {
diff = Math.round(diff / 86400);
if (diff == 1) {
display_date = diff + ' ' + dictionary.day;
} else {
display_date = diff + ' ' + dictionary.days;
}
}
return display_date;
}
其他回答
我也有同样的问题,但如果你在SQL查询上做的话会更好:
DateDiff(DAY, StartValue,GETDATE()) AS CountDays
查询将自动生成一个列CountDays
下面的解决方案将假设这些变量在代码中可用:
const startDate = '2020-01-01';
const endDate = '2020-03-15';
原生 JS
步骤:
设定开始日期 设定结束日期 计算的区别 将毫秒转换为天
const diffInMs = new Date(endDate) - new Date(startDate)
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
备注:
我知道这不是你问题的一部分,但一般来说,我不建议在香草JavaScript中做任何日期计算或操作,而是使用date-fns, Luxon或moment.js这样的库,因为有许多边缘情况。
这个简单的JavaScript回答以十进制数计算天数。此外,在使用夏令时时,它可能会遇到边缘情况
使用图书馆
- 日期-fns
const differenceInDays = require('date-fns/differenceInDays');
const diffInDays = differenceInDays(new Date(endDate), new Date(startDate));
文档:https://date-fns.org/v2.16.1/docs/differenceInDays
——国际光子
const { DateTime } = require('luxon');
const diffInDays = DateTime.fromISO(endDate).diff(DateTime.fromISO(startDate), 'days').toObject().days;
文档:https://moment.github.io/luxon/docs/class/src/datetime.js DateTime.html # instance-method-diff
——Moment.js
const moment = require('moment');
const diffInDays = moment(endDate).diff(moment(startDate), 'days');
文档:https://momentjs.com/docs/ / /显示/不同
RunKit示例
夏令时问题使这里的许多答案无效。我将使用一个helper函数来获得给定日期的唯一天数——通过使用UTC方法:
const dayNumber = a => Date.UTC(a.getFullYear(), a.getMonth(), a.getDate()) / (24*60*60*1000); const daysBetween = (a, b) => dayNumber(b) - dayNumber(a); // Testing it const start = new Date(1000, 0, 1); // 1 January 1000 const end = new Date(3000, 0, 1); // 1 January 3000 let current = new Date(start); for (let days = 0; current < end; days++) { const diff = daysBetween(start, current); if (diff !== days) throw "test failed"; current.setDate(current.getDate() + 1); // move current date one day forward } console.log("tests succeeded");
我在Angular中也遇到了同样的问题。我复制了一份,否则他会覆盖第一次约会。两个日期的时间都必须为00:00:00(显然)
/*
* Deze functie gebruiken we om het aantal dagen te bereken van een booking.
* */
$scope.berekenDagen = function ()
{
$scope.booking.aantalDagen=0;
/*De loper is gelijk aan de startdag van je reservatie.
* De copy is nodig anders overschijft angular de booking.van.
* */
var loper = angular.copy($scope.booking.van);
/*Zolang de reservatie beschikbaar is, doorloop de weekdagen van je start tot einddatum.*/
while (loper < $scope.booking.tot) {
/*Tel een dag op bij je loper.*/
loper.setDate(loper.getDate() + 1);
$scope.booking.aantalDagen++;
}
/*Start datum telt natuurlijk ook mee*/
$scope.booking.aantalDagen++;
$scope.infomsg +=" aantal dagen: "+$scope.booking.aantalDagen;
};
一行代码和小代码
const diff=(e,t)=>Math.floor((new Date(e).getTime()-new Date(t).getTime())/1000*60*60*24);
// or
const diff=(e,t)=>Math.floor((new Date(e)-new Date(t))/864e5);
// or
const diff=(a,b)=>(new Date(a)-new Date(b))/864e5|0;
// use
diff('1/1/2001', '1/1/2000')
为打印稿
const diff = (from: string, to: string) => Math.floor((new Date(from).getTime() - new Date(to).getTime()) / 86400000);