【发布时间】:2015-04-16 13:16:13
【问题描述】:
您好,我试图找出两个日期之间的差异,但我一直得到 0 作为答案,任何人都知道为什么请帮忙。我的代码是这样的
var d1 = "2015-04-30";
var d2 = "2015-04-14";
var startDay = new Date(d1);
var endDay = new Date(d2);
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = startDay.getTime() - endDay.getTime();
var days = millisBetween / millisecondsPerDay;
//If I use df1 and df2 I am getting 0
var jsondate1 = new Date(getData.startDate).toISOString();
var jsondate2 = new Date(getData.startDate).toISOString();
var date = new Date(jsondate1);
var dates = new Date(jsondate2);
var df1 = date.getFullYear() + '-' + (date.getMonth()+1) + '-'
+date.getDate() ;
var df2 = dates.getFullYear() + '-' +(dates.getMonth()+1)
+ '-' +dates.getDate() ;
//If I am using the below two lines the answer is 16 but if I am using the above d1 and d2 the answer is zero
var d1 = "2015-4-30";// the date here is made up by console.debug(df1 )
var d2 = "2015-4-14"; // the date here is made up by console.debug(df2 )
我不知道我哪里做错了
【问题讨论】:
-
不,我的答案是 0,我不知道是什么
-
days = 16。你从哪里得到 0?你能告诉我们你是如何看待你的结果的吗?
-
console.debug(days) = 0
-
确认
16为days变量。
标签: javascript date