This article describes the method of JavaScript to compare whether the current time is within a specified time period. Share it for your reference, as follows:
function checkTime(stime, etime) { //Start time var arrrs = stime.split("-"); var startTime = new Date(arrs[0], arrrs[1], arrrs[2]); var startTimes = startTime.getTime(); //End time var arr = etime.split("-"); var endTime = new Date(arre[0], arr[1], arr[2]); var endTimes = endTime.getTime(); //Current time 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;}//Usage: var timebool=checkTime('2016-8-1','2016-8-10');//Note: Dates are separated by "-" if(timebool==true){ document.write('The current date is within the specified time period');}else{ document.write('The current date is not within the specified time period');}PS: Friends who are interested in JavaScript time and date operations can also refer to the online tools of this site:
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
Online time inquiry around the world:
http://tools.VeVB.COM/zhuanhuanqi/worldtime
Online Perpetual Calendar:
http://tools.VeVB.COM/bianmin/wannianli
Web Perpetual Calendar:
http://tools.VeVB.COM/bianmin/webwannianli
For more information about JavaScript, please check this site's special topics: "Summary of JavaScript Time and Date Operation Skills", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Data Structures and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"
I hope this article will be helpful to everyone's JavaScript programming.