我使用以下JS函数来计算今天和未来一天之间的天数差异:
var oneDay = 24 * 60 * 60 * 1000;
var today = new Date();
var futureDay = new Date(futureDate);
var diffDays = Math.round(Math.abs((today.getTime() - futureDay.getTime()) / (oneDay)));
我的问题:当futureDate今天为止时,我得到结果“1”,如果是明天,我得到“0”。
这个功能有什么问题?
答案 0 :(得分:0)
尝试这种方式,您没有设置future date
var oneDay=1000 * 3600 * 24;
var dateToday = new Date("02/24/2015"); //or just, new Date();
var dateFuture = new Date("02/25/2015"); // set here future date like this
var timeDiff = Math.abs(dateFuture.getTime() - dateToday.getTime());
var diffDays = Math.ceil(timeDiff /oneDay);
alert(diffDays);