JS比較當前時間是否在指定時間段內


function checkTime(stime, etime) {
   //開始時間
   var arrs = stime.split( "-" );
   var startTime = new Date(arrs[0], arrs[1], arrs[2]);
   var startTimes = startTime.getTime();
   //結束時間
   var arre = etime.split( "-" );
   var endTime = new Date(arre[0], arre[1], arre[2]);
   var endTimes = endTime.getTime();
   //當前時間
   var thisDate = new Date();
   var thisDates = thisDate.getFullYear() + "-0" + (thisDate.getMonth() + 1) + "-" + thisDate.getDate();
   var arrn = thisDates.split( "-" );
   var nowTime = new Date(arrn[0], arrn[1], arrn[2]);
   var nowTimes = nowTime.getTime();
   if (nowTimes < startTimes || nowTimes > endTimes) {
     return false ;
   }
   return true ;
}
//用法:
var timebool=checkTime( '2016-8-1' , '2016-8-10' ); //注意:日期用“-”分隔
if (timebool== true ){
   document.write( '當前日期在指定時間段內' );
} else {
   document.write( '當前日期不在指定時間段內' );
}


免責聲明!

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



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