2012-06-04 31 views
0

比較如何比較兩個日期這樣的格式字符串轉換爲Date對象,並在JS

 var CurrDate = new Date().format("MM/dd/yyyy"); 
    if (Date.parse("05-Jun-2012")>Date.parse(CurrDate)) 
     { 
     alert("Please enter future date!"); 
     return false; 
     } 

請幫忙確認日期。

+0

http://stackoverflow.com/questions/3509683/validate-two-dates-of-this-dd-mmm-yyyy-format-in-javascript 重複的問題。在這裏找到你的答案。 – Neha

回答

1
function customParse(str) { 
    var months = ['Jan','Feb','Mar','Apr','May','Jun', 
       'Jul','Aug','Sep','Oct','Nov','Dec'], 
     n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches; 

    while(n--) { months[months[n]]=n; } // map month names to their index :) 

    matches = str.match(re); // extract date parts from string 

    return new Date(matches[3], months[matches[2]], matches[1]); 
} 

customParse("18-Aug-2010"); 
// "Wed Aug 18 2010 00:00:00" 

customParse("19-Aug-2010") > customParse("18-Aug-2010"); 
+0

感謝Neha..its工作正常,我想與當前日期也可以.. ..可能嗎? – Justinonday