我正在處理2個日期,這些日期在文本框中以這種格式作爲字符串發佈給我03/02/2010。一個是當前完成日期,第二個是最終完成日期。我需要比較這兩個日期,以檢查最終完成日期是否在當前完成日期的前面,後面或相同。如何查看2個日期字段並比較查看哪個日期在前面,後面或相同
有沒有一種方法可以使用JavaScript或jQuery來做到這一點?
感謝您的任何幫助。
我正在處理2個日期,這些日期在文本框中以這種格式作爲字符串發佈給我03/02/2010。一個是當前完成日期,第二個是最終完成日期。我需要比較這兩個日期,以檢查最終完成日期是否在當前完成日期的前面,後面或相同。如何查看2個日期字段並比較查看哪個日期在前面,後面或相同
有沒有一種方法可以使用JavaScript或jQuery來做到這一點?
感謝您的任何幫助。
var passedDate1 = new Date('03/02/2010');
var passedDate2 = new Date('03/01/2010');
if (passedDate1 > passedDate2) {
alert ('Date1 is greated than date 2');
}
else if (passedDate1 < passedDate2) {
alert ('Date1 is less than date 2');
}
else {
alert ('they are equal');
}
將其轉換爲美國格式
function dateUS(date)
{
var date = date.split("/");
return date[2] + '/' + date[1] + '/' + date[0];
}
然後
if(dateUS(dateCurrent) < dateUS(dateFinal))
{
//your code
}
var doc = document,
dateBox1 = doc.getElementById("date1").value,
dateBox2 = doc.getElementById("date2").value,
d1, d2, diff;
//if there is no value, Date() would return today
if (dateBox1) {
d1 = new Date(dateBox1);
} else {
//however you want to handle missing date1
}
if (dateBox1) {
d2 = new Date(dateBox2);
} else {
//however you want to handle missing date2
}
if (d1 && d2) {
//reduce the difference to days in absolute value
diff = Math.floor(Math.abs((d1 - d2) /1000/60/60/24));
} else {
//handle not having both dates
}
if (diff === 0) {
//d1 and d2 are the same day
}
if (diff && d1 > d2) {
//d1 is diff days after d2 and the diff is not zero
}
if (diff && d1 < d2) {
//d1 is diff days before d2 and the diff is not zero
}
非常感謝約翰,這正是我需要的東西! – Cliftwalker 2010-09-27 10:25:28