可能重复:
How do I get the difference between two Dates in JavaScript?
如何使用javascript获取两个日期之间的差异。我需要这两个日期之间的确切日差。这是我的代码
function calculateDifference() {
var startDate = document.getElementById("MainContent_txtOStartDate").value;
var endDate = document.getElementById("MainContent_txtOEndDate").value;
return DateDiff(startDate, endDate);
}
function DateDiff(startDate, endDate) {
var a = Date.parse(startDate) - Date.parse(endDate);
alert(a);
}
有什么建议吗?
答案 0 :(得分:1)
var date1 = new Date(2012, 2, 28, 1, 1, 1);
var date2 = new Date(2012, 2, 22, 1, 1, 1);
alert(date1);
alert(date2);
var sec = 1000;
var min = sec * 60;
var hour = min * 60;
var day = hour * 24;
var dateDiff = (date1 - date2) / day;
alert('Difference in days: ' + dateDiff);