2012-03-28 95 views
1

可能重複:
How do I get the difference between two Dates in JavaScript?如何獲得兩個日期之間的差異?

如何獲得使用javascript.I需要這兩個dates.Here之間的確切日期相差兩個日期之間的區別是我的代碼

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); 
} 

有什麼建議嗎?

+7

哦,太好了一個新的問題!很確定我從來沒有見過這樣的! – talnicolas 2012-03-28 12:58:29

+0

http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript – 2012-03-28 12:59:29

+0

這裏查看我的回答http://stackoverflow.com/questions/6829061 /需-A-的JavaScript到比較-日期 – Dotnet 2012-03-28 13:03:18

回答

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); 

​