我遇到DateDiff函数的问题.我想弄清楚两个日期/时间之间的差异.我已经阅读了这篇文章(在Javascript中计算日期差异的最佳方法是什么),我也看了这个教程(http://www.javascriptkit.com/javatutors/datedifference.shtml),但我似乎无法得到它.
这是我试图开始工作但没有成功.有人可以告诉我我在做什么以及如何简化这一点.似乎有点过度编码......?
//Set the two dates
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currDate = month + "/" + day + "/" + year;
var iniremDate = "8/10/2012";
//Show the dates subtracted
document.write('DateDiff is: ' + currDate - iniremDate);
//Try this function...
function DateDiff(date1, date2) {
return date1.getTime() - date2.getTime();
}
//Print the results of DateDiff
document.write (DateDiff(iniremDate, currDate);
Run Code Online (Sandbox Code Playgroud)