JavaScript比较当前时间是否在指定时间段内的方法

本文实例讲述了JavaScript比较当前时间是否在指定时间段内的方法。分享给大家供大家参考,具体如下:

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('当前日期不在指定时间段内'); }

PS:对JavaScript时间与日期操作感兴趣的朋友还可以参考本站在线工具:

Unix时间戳(timestamp)转换工具:

在线世界各地时间查询:

在线万年历日历:

网页万年历日历:

更多关于JavaScript相关内容可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结

希望本文所述对大家JavaScript程序设计有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzxfxf.html