function checkTime(stime,etime){
//通過replace方法將字符串轉換成Date格式 var sdate= new Date(Date.parse(stime.replace(/-/g, "/"))); var edate= new Date(Date.parse(etime.replace(/-/g, "/"))); //獲取兩個日期的年月日 var smonth=sdate.getMonth()+1; var syear =sdate.getFullYear(); var sday = sdate.getDate(); var emonth=edate.getMonth()+1; var eyear =edate.getFullYear(); var eday = edate.getDate(); //從年,月,日,分別進行比較 if(syear>eyear){ return false; }else{ if(smonth>emonth){ return false; }else{ if(sday>eday){ return false; }else{ return true; } } } }
因為已經轉換成了Date格式,也可以用getTime()方法進行比較
return sdate.getTime()>edate.getTime()
轉載自:http://www.oschina.net/code/snippet_2426852_49909
