分享一段判斷時間是否在某時間段內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("娛樂"); }