我将开始日期和结束日期转换为.ToLocalString,现在我尝试使用math.abs计算数字中开始日期和结束日期之间的差,但其值为NaN。在这种情况下如何应用Math.abs的任何建议都将受到赞赏。
注意:开始日期将在EDT中,而结束日期将在EST中。但是他们 可能或可能不在同一时区。
var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.toLocaleString();
var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.toLocaleString();
var timeDiff = Math.abs(endDate - startDate); //This is NaN
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Here it will add 1 extra day. Example: 11/06/2018 - 11/04/2018 = 2 days but this gives 3days are timezone change on 11/04/2018 and thats the issue.
答案 0 :(得分:0)
我会使用unix时间戳。
var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.getTime();
var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.getTime();
var timeDiff = Math.abs(endDate - startDate);