JavaScript比較當前時間是否在指定時間段內(nèi)的方法
本文實例講述了JavaScript比較當前時間是否在指定時間段內(nèi)的方法。分享給大家供大家參考,具體如下:
function checkTime(stime, etime) {
//開始時間
var arrs = stime.split("-");
var startTime = new Date(arrs[0], arrs[1], arrs[2]);
var startTimes = startTime.getTime();
//結(jié)束時間
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('當前日期在指定時間段內(nèi)');
}else{
document.write('當前日期不在指定時間段內(nèi)');
}
PS:對JavaScript時間與日期操作感興趣的朋友還可以參考本站在線工具:
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
在線世界各地時間查詢:
http://tools.jb51.net/zhuanhuanqi/worldtime
在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli
網(wǎng)頁萬年歷日歷:
http://tools.jb51.net/bianmin/webwannianli
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript時間與日期操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
Promise+async+Generator的實現(xiàn)原理
這篇文章主要介紹了Promise+async+Generator的實現(xiàn)原理,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09

