如何获取JavaScript中两个日期之间的天数?

如何获取JavaScript中两个日期之间的天数?

如何获取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>


翻阅古今
浏览 972回答 4
4回答

收到一只叮咚

在撰写本文时,只有其中一个答案正确处理DST(夏令时)转换。以下是位于加利福尼亚州的系统的结果:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1/1/2013- 3/10/2013- 11/3/2013-User&nbsp; &nbsp; &nbsp; &nbsp;Formula&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2/1/2013&nbsp; 3/11/2013&nbsp; 11/4/2013&nbsp; Result---------&nbsp; ---------------------------&nbsp; --------&nbsp; ---------&nbsp; ---------&nbsp; ---------Miles&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(d2 - d1) / N&nbsp; &nbsp;31&nbsp; &nbsp; &nbsp; &nbsp; 0.9583333&nbsp; 1.0416666&nbsp; Incorrectsome&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Math.floor((d2 - d1) / N)&nbsp; 31&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Incorrectfuentesjr&nbsp; &nbsp; Math.round((d2 - d1) / N)&nbsp; 31&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Correcttoloco&nbsp; &nbsp; &nbsp;Math.ceiling((d2 - d1) / N)&nbsp; 31&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IncorrectN = 86400000虽然Math.round返回了正确的结果,但我认为它有些笨重。相反,通过在DST开始或结束时明确说明UTC偏移的变化,我们可以使用精确算术:function treatAsUTC(date) {&nbsp; &nbsp; var result = new Date(date);&nbsp; &nbsp; result.setMinutes(result.getMinutes() - result.getTimezoneOffset());&nbsp; &nbsp; return result;}function daysBetween(startDate, endDate) {&nbsp; &nbsp; var millisecondsPerDay = 24 * 60 * 60 * 1000;&nbsp; &nbsp; 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。

暮色呼如

获得两个日期之间差异的最简单方法:var diff =&nbsp; Math.floor(( Date.parse(str2) - Date.parse(str1) ) / 86400000);&nbsp;您可以获得差异天数(如果无法解析其中一个或两个,则为NaN)。解析日期以毫秒为单位给出结果,并且按天划分它需要将其除以24 * 60 * 60 * 1000如果你想要它除以天,小时,分钟,秒和毫秒:function dateDiff( str1, str2 ) {&nbsp; &nbsp; var diff = Date.parse( str2 ) - Date.parse( str1 );&nbsp;&nbsp; &nbsp; return isNaN( diff ) ? NaN : {&nbsp; &nbsp; &nbsp; &nbsp; diff : diff,&nbsp; &nbsp; &nbsp; &nbsp; ms : Math.floor( diff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; % 1000 ),&nbsp; &nbsp; &nbsp; &nbsp; s&nbsp; : Math.floor( diff /&nbsp; &nbsp; &nbsp;1000 %&nbsp; &nbsp;60 ),&nbsp; &nbsp; &nbsp; &nbsp; m&nbsp; : Math.floor( diff /&nbsp; &nbsp; 60000 %&nbsp; &nbsp;60 ),&nbsp; &nbsp; &nbsp; &nbsp; h&nbsp; : Math.floor( diff /&nbsp; 3600000 %&nbsp; &nbsp;24 ),&nbsp; &nbsp; &nbsp; &nbsp; d&nbsp; : Math.floor( diff / 86400000&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; };}这是我的重构版James版本:function mydiff(date1,date2,interval) {&nbsp; &nbsp; var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;&nbsp; &nbsp; date1 = new Date(date1);&nbsp; &nbsp; date2 = new Date(date2);&nbsp; &nbsp; var timediff = date2 - date1;&nbsp; &nbsp; if (isNaN(timediff)) return NaN;&nbsp; &nbsp; switch (interval) {&nbsp; &nbsp; &nbsp; &nbsp; case "years": return date2.getFullYear() - date1.getFullYear();&nbsp; &nbsp; &nbsp; &nbsp; case "months": return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( date2.getFullYear() * 12 + date2.getMonth() )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( date1.getFullYear() * 12 + date1.getMonth() )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; case "weeks"&nbsp; : return Math.floor(timediff / week);&nbsp; &nbsp; &nbsp; &nbsp; case "days"&nbsp; &nbsp;: return Math.floor(timediff / day);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; case "hours"&nbsp; : return Math.floor(timediff / hour);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; case "minutes": return Math.floor(timediff / minute);&nbsp; &nbsp; &nbsp; &nbsp; case "seconds": return Math.floor(timediff / second);&nbsp; &nbsp; &nbsp; &nbsp; default: return undefined;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript