在下面的代码中我得到了course_diff_date
作为NaN我在这里做错了什么,我想在天里得到差异
console.log(d1);
//Thu Jan 01 1970 05:30:00 GMT+0530 (IST)
var today = new Date();
var course_time = new Date(d1);
var new_date = new Date(today.getMonth()+ "/" +today.getDate()+ "/" +today.getFullYear());
var server_date = new Date(course_time.getMonth()+ "/" +course_time.getDate()+ "/" +course_time.getFullYear());
var timeDiff = Math.round(new_date.getTime() - server_date.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
course_diff_date = diffDays + " Days";
答案 0 :(得分:1)
请尝试使用date.getTime()
:
修改强>
var daysTillNow = function(str) {
var today = new Date();
//time stamp is taken for testing
var course_time = new Date(str);
//difference in mili seconds
var diff = today.getTime() - course_time.getTime();
//round off mili-sec to days
diff = Math.round(diff / (1000 * 60 * 60 * 24));
return diff + " day(s)";
};
console.log(daysTillNow('Thu Dec 11 2014 05:30:00 GMT+0530 (IST)')); //11 days
console.log(daysTillNow('Mon Dec 22 2014 11:13:21 GMT+0530 (IST)')); //0 days
open console... F12
答案 1 :(得分:0)
而不是
course_time.time.getMonth()
使用:
course_time.getMonth()
答案 2 :(得分:0)
你有额外的.time
var server_date = new Date(course_time.time.getMonth()+ "/" +course_time.getDate()+ "/" +course_time.getFullYear());
应该是
var server_date = new Date(course_time.getMonth()+ "/" +course_time.getDate()+ "/" +course_time.getFullYear());
答案 3 :(得分:0)
应该是course_time而不是course_time.time
d1 = "12 January 2014";
d1 = new Date(d1);
var today = new Date();
var course_time = new Date(d1);
var new_date = new Date(today.getMonth()+ "/" +today.getDate()+ "/" +today.getFullYear());
var server_date = new Date(course_time.getMonth()+ "/" +course_time.getDate()+ "/" +course_time.getFullYear());
var timeDiff = Math.round(new_date.getTime() - server_date.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
course_diff_date = diffDays + " Days";