如何获取JavaScript中两个日期之间的天数?例如,在输入框中给出两个日期:
Miles.. 390
这是一个快速而肮脏的实现datediff
,作为解决问题中提出的问题的概念证明.它依赖于这样一个事实:你可以通过减去它们来获得两个日期之间经过的毫秒数,这会将它们强制转换为它们的原始数值(自1970年开始以来的毫秒数).
// new Date("dateString") is browser-dependent and discouraged, so we'll write
// a simple parse function for U.S. date format (which does no error checking)
function parseDate(str) {
var mdy = str.split('/');
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24));
}
alert(datediff(parseDate(first.value), parseDate(second.value)));
您应该知道"普通"日期API(名称中没有"UTC")在用户浏览器的本地时区运行,因此一般情况下,如果您的用户位于您不在的时区,则可能会遇到问题期待,您的代码将不得不处理夏令时转换.您应该仔细阅读Date对象及其方法的文档,对于任何更复杂的文档,强烈考虑使用提供更安全和强大的API来进行日期操作的库.
数字和日期 - MDN JavaScript指南
Date
- MDN JavaScript参考
此外,为了便于说明,该代码段使用了window
对象的命名访问权限,但在生产中,您应该使用标准化的API,如getElementById,或者更可能使用某些UI框架.
这是一个快速而肮脏的实现datediff
,作为解决问题中提出的问题的概念证明.它依赖于这样一个事实:你可以通过减去它们来获得两个日期之间经过的毫秒数,这会将它们强制转换为它们的原始数值(自1970年开始以来的毫秒数).
// new Date("dateString") is browser-dependent and discouraged, so we'll write
// a simple parse function for U.S. date format (which does no error checking)
function parseDate(str) {
var mdy = str.split('/');
return new Date(mdy[2], mdy[0]-1, mdy[1]);
}
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
return Math.round((second-first)/(1000*60*60*24));
}
alert(datediff(parseDate(first.value), parseDate(second.value)));
在撰写本文时,只有其中一个答案正确处理DST(夏令时)转换.以下是位于加利福尼亚州的系统的结果:
1/1/2013- 3/10/2013- 11/3/2013- User Formula 2/1/2013 3/11/2013 11/4/2013 Result --------- --------------------------- -------- --------- --------- --------- Miles (d2 - d1) / N 31 0.9583333 1.0416666 Incorrect some Math.floor((d2 - d1) / N) 31 0 1 Incorrect fuentesjr Math.round((d2 - d1) / N) 31 1 1 Correct toloco Math.ceiling((d2 - d1) / N) 31 1 2 Incorrect N = 86400000
虽然Math.round
返回了正确的结果,但我认为它有些笨重.相反,通过在DST开始或结束时明确说明UTC偏移的变化,我们可以使用精确算术:
function treatAsUTC(date) { var result = new Date(date); result.setMinutes(result.getMinutes() - result.getTimezoneOffset()); return result; } function daysBetween(startDate, endDate) { var millisecondsPerDay = 24 * 60 * 60 * 1000; return (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay; } alert(daysBetween($('#first').val(), $('#second').val()));
JavaScript日期计算很棘手,因为Date
对象在UTC内部存储时间,而不是本地时间.例如,3/10/2013 12:00 AM太平洋标准时间(UTC-08:00)存储为3/10/2013 8:00 AM UTC和3/11/2013 12:00 AM Pacific Daylight Time( UTC-07:00)存储为3/11/2013 7:00 AM UTC.在这一天午夜到午夜当地时间只有23小时在UTC!
虽然当地时间的一天可能有多于或少于24小时,但UTC的一天总是正好24小时.1daysBetween
上面显示的方法利用了这一事实,首先要求treatAsUTC
在减去和分割之前调整本地时间到午夜UTC.
1. JavaScript忽略了闰秒.
获得两个日期之间差异的最简单方法:
var diff = Math.floor(( Date.parse(str2) - Date.parse(str1) ) / 86400000);
您可以获得差异天数(如果无法解析其中一个或两个,则为NaN).解析日期以毫秒为单位给出结果,并且按天划分它需要将其除以24*60*60*1000
如果你想要它除以天,小时,分钟,秒和毫秒:
function dateDiff( str1, str2 ) { var diff = Date.parse( str2 ) - Date.parse( str1 ); return isNaN( diff ) ? NaN : { diff : diff, ms : Math.floor( diff % 1000 ), s : Math.floor( diff / 1000 % 60 ), m : Math.floor( diff / 60000 % 60 ), h : Math.floor( diff / 3600000 % 24 ), d : Math.floor( diff / 86400000 ) }; }
这是我的重构版James版本:
function mydiff(date1,date2,interval) { var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7; date1 = new Date(date1); date2 = new Date(date2); var timediff = date2 - date1; if (isNaN(timediff)) return NaN; switch (interval) { case "years": return date2.getFullYear() - date1.getFullYear(); case "months": return ( ( date2.getFullYear() * 12 + date2.getMonth() ) - ( date1.getFullYear() * 12 + date1.getMonth() ) ); case "weeks" : return Math.floor(timediff / week); case "days" : return Math.floor(timediff / day); case "hours" : return Math.floor(timediff / hour); case "minutes": return Math.floor(timediff / minute); case "seconds": return Math.floor(timediff / second); default: return undefined; } }
我建议使用moment.js库(http://momentjs.com/docs/#/displaying/difference/).它正确处理夏令时,一般来说都很适合.
例:
var start = moment("2013-11-03"); var end = moment("2013-11-04"); end.diff(start, "days") 1
我会继续抓住这个小实用程序,在其中你会找到适合你的功能.这是一个简短的例子:
const startDate = '2017-11-08'; const endDate = '2017-10-01'; const timeDiff = (new Date(startDate)) - (new Date(endDate)); const days = timeDiff / (1000 * 60 * 60 * 24)
设置开始日期
设置结束日期
计算差异
将毫秒转换为天
使用Moment.js
var future = moment('05/02/2015');
var start = moment('04/23/2015');
var d = future.diff(start, 'days'); // 9
console.log(d);
JS中的日期值是日期时间值.
因此,直接日期计算不一致:
(2013-11-05 00:00:00) - (2013-11-04 10:10:10) < 1 day
例如,我们需要转换第二个日期:
(2013-11-05 00:00:00) - (2013-11-04 00:00:00) = 1 day
该方法可能会截断两个日期的工厂:
var date1 = new Date('2013/11/04 00:00:00');
var date2 = new Date('2013/11/04 10:10:10'); //less than 1
var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
var daysDiff = end - start; // exact dates
console.log(daysDiff);
date2 = new Date('2013/11/05 00:00:00'); //1
var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
var daysDiff = end - start; // exact dates
console.log(daysDiff);
最好通过使用UTC时间摆脱DST,Math.ceil,Math.floor等:
var firstDate = Date.UTC(2015,01,2); var secondDate = Date.UTC(2015,04,22); var diff = Math.abs((firstDate.valueOf() - secondDate.valueOf())/(24*60*60*1000));
这个例子给出了差异109天.24*60*60*1000
是一天,以毫秒为单位.
要计算2个给定日期之间的天数,您可以使用以下代码.我在这里使用的日期是2016年1月1日和2016年12月31日
var day_start = new Date("Jan 01 2016");
var day_end = new Date("Dec 31 2016");
var total_days = (day_end - day_start) / (1000 * 60 * 60 * 24);
document.getElementById("demo").innerHTML = Math.round(total_days);
DAYS BETWEEN GIVEN DATES
可以使用以下公式计算在不同TZ之间休息的两个日期之间的完整证明天差:
var start = new Date('10/3/2015');
var end = new Date('11/2/2015');
var days = (end - start) / 1000 / 60 / 60 / 24;
console.log(days);
// actually its 30 ; but due to daylight savings will show 31.0xxx
// which you need to offset as below
days = days - (end.getTimezoneOffset() - start.getTimezoneOffset()) / (60 * 24);
console.log(days);
我认为解决方案不正确100%我会使用ceil而不是floor,round会工作但是它不是正确的操作.
function dateDiff(str1, str2){ var diff = Date.parse(str2) - Date.parse(str1); return isNaN(diff) ? NaN : { diff: diff, ms: Math.ceil(diff % 1000), s: Math.ceil(diff / 1000 % 60), m: Math.ceil(diff / 60000 % 60), h: Math.ceil(diff / 3600000 % 24), d: Math.ceil(diff / 86400000) }; }
当我想在两个日期做一些计算时,我发现了这个问题,但日期有小时和分钟值,我修改了@ michael-liu的答案以符合我的要求,并且它通过了我的测试.
DIFF天2012-12-31 23:00
并2013-01-01 01:00
应等于1.(2小时)的差异天2012-12-31 01:00
并2013-01-01 23:00
应等于1(46小时)的
function treatAsUTC(date) { var result = new Date(date); result.setMinutes(result.getMinutes() - result.getTimezoneOffset()); return result; } var millisecondsPerDay = 24 * 60 * 60 * 1000; function diffDays(startDate, endDate) { return Math.floor(treatAsUTC(endDate) / millisecondsPerDay) - Math.floor(treatAsUTC(startDate) / millisecondsPerDay); }
var start= $("#firstDate").datepicker("getDate"); var end= $("#SecondDate").datepicker("getDate"); var days = (end- start) / (1000 * 60 * 60 * 24); alert(Math.round(days));
jsfiddle示例:)
如何在DatePicker小部件中使用formatDate呢?您可以使用它来转换时间戳格式的日期(自1970年1月1日起的毫秒),然后进行简单的减法.
function timeDifference(date1, date2) {
var oneDay = 24 * 60 * 60; // hours*minutes*seconds
var oneHour = 60 * 60; // minutes*seconds
var oneMinute = 60; // 60 seconds
var firstDate = date1.getTime(); // convert to milliseconds
var secondDate = date2.getTime(); // convert to milliseconds
var seconds = Math.round(Math.abs(firstDate - secondDate) / 1000); //calculate the diffrence in seconds
// the difference object
var difference = {
"days": 0,
"hours": 0,
"minutes": 0,
"seconds": 0,
}
//calculate all the days and substract it from the total
while (seconds >= oneDay) {
difference.days++;
seconds -= oneDay;
}
//calculate all the remaining hours then substract it from the total
while (seconds >= oneHour) {
difference.hours++;
seconds -= oneHour;
}
//calculate all the remaining minutes then substract it from the total
while (seconds >= oneMinute) {
difference.minutes++;
seconds -= oneMinute;
}
//the remaining seconds :
difference.seconds = seconds;
//return the difference object
return difference;
}
console.log(timeDifference(new Date(2017,0,1,0,0,0),new Date()));