给我写两个日期相减的js代码
时间: 2024-05-21 15:11:53 浏览: 118
JS 两日期相减,获得天数的小例子(兼容IE,FF)
以下是两个日期相减的JavaScript代码:
// 方法一:
var date1 = new Date("2021-05-01");
var date2 = new Date("2021-05-10");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(diffDays);
// 方法二:
var date1 = new Date("2021-05-01");
var date2 = new Date("2021-05-10");
var diffTime = date2.getTime() - date1.getTime();
var diffDays = diffTime / (1000 * 3600 * 24);
console.log(Math.ceil(diffDays));
阅读全文