我如何获取两个日期之间的完整天数差异(不要有任何一天的零头)?
var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getDate() - date1.getDate();
alert(diffDays)
我尝试了上面的方法,但它没有起作用。我如何获取两个日期之间的完整天数差异(不要有任何一天的零头)?
var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getDate() - date1.getDate();
alert(diffDays)
我尝试了上面的方法,但它没有起作用。const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");
注意需要用引号括起日期。代码的其余部分获取以毫秒为单位的时间差,然后进行除法运算以获取天数。Date 类型的日期格式应为 mm/dd/yyyy。
getDate()
返回的是不考虑月份的日期,因此您将获得1天。因此,您将得到12-11 = 1。 - TNi... 由于日期本身自带时区信息,可能跨越有不同日光节约调整的地区
以前对这个问题的回答没有考虑到两个日期跨越夏令时(DST)变化的情况。发生DST变化的日期在毫秒级别上的持续时间 != 1000 * 60 * 60 * 24
,所以常规计算方法会失败。
您可以通过先将两个日期转换为UTC标准时间,然后计算这两个UTC日期之间的差异来解决此问题。
现在,解决方案可以写成:
// a and b are javascript Date objects
function dateDiffInDays(a, b) {
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
// test it
const a = new Date("2017-01-01"),
b = new Date("2017-07-25"),
difference = dateDiffInDays(a, b);
console.log(difference + ' days')
这是因为UTC时间从不遵守夏令时。参见Does UTC observe daylight saving time?
p.s. 经过讨论评论后,一旦您了解跨越夏令时边界的JavaScript日期问题,可能有不止一种解决方法。我提供的是一个简单(并经过测试)的解决方案。如果有一种简单的基于算术/数学的解决方案而不必实例化两个新的Date对象,那将可能更快。
Math.round((b - a)/ _MS_PER_DAY, 0)
,因此很难支持“唯一正确的答案”这个说法。 - Scott Sauyetvar date1 = new Date("7/11/2010");
var date2 = new Date("8/11/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);
alert(diffDays )
(date2 - date1) / (1000 * 60 * 60 * 24) | 0
。 - Dagg Nabbit我尝试了很多方法,发现使用datepicker是最好的选择,但日期格式会导致JavaScript出现问题....
因此,这里是我的答案,可以直接运行。
<input type="text" id="startdate">
<input type="text" id="enddate">
<input type="text" id="days">
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css" />
<script>
$(document).ready(function() {
$( "#startdate,#enddate" ).datepicker({
changeMonth: true,
changeYear: true,
firstDay: 1,
dateFormat: 'dd/mm/yy',
})
$( "#startdate" ).datepicker({ dateFormat: 'dd-mm-yy' });
$( "#enddate" ).datepicker({ dateFormat: 'dd-mm-yy' });
$('#enddate').change(function() {
var start = $('#startdate').datepicker('getDate');
var end = $('#enddate').datepicker('getDate');
if (start<end) {
var days = (end - start)/1000/60/60/24;
$('#days').val(days);
}
else {
alert ("You cant come back before you have been!");
$('#startdate').val("");
$('#enddate').val("");
$('#days').val("");
}
}); //end change function
}); //end ready
</script>
这里可以看到一个小小的演示DEMO
var dat1 = document.getElementById('inputDate').value;
var date1 = new Date(dat1)//converts string to date object
alert(date1);
var dat2 = document.getElementById('inputFinishDate').value;
var date2 = new Date(dat2)
alert(date2);
var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.abs((date1.getTime() - date2.getTime()) / (oneDay));
alert(diffDays);
new Date
,例如new Date(2010, 11, 7);
。 - Marcel Korpel