分享一段判断时间是否在某时间段内js函数,JS判断当前“时 : 分”是否在一天中某一区间内。比如:15.30 在不在08.30到16.00之间。
js函数代码
function checkAuditTime(beginTime, endTime) { var nowDate = new Date(); var beginDate = new Date(nowDate); var endDate = new Date(nowDate); var beginIndex = beginTime.lastIndexOf("\:"); var beginHour = beginTime.substring(0, beginIndex); var beginMinue = beginTime.substring(beginIndex + 1, beginTime.length); beginDate.setHours(beginHour, beginMinue, 0, 0); var endIndex = endTime.lastIndexOf("\:"); var endHour = endTime.substring(0, endIndex); var endMinue = endTime.substring(endIndex + 1, endTime.length); endDate.setHours(endHour, endMinue, 0, 0); return nowDate.getTime() - beginDate.getTime() >= 0 && nowDate.getTime() <= endDate.getTime(); } checkAuditTime("8:30", "16:00");
函数使用方法
if (checkAuditTime("12:00", "13:00") || checkAuditTime("17:30", "19:30")) { alert("休息吃饭时间"); } else { alert("娱乐"); }