在js中比較"yyyy-MM-dd"格式日期大小


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM