js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
程序员文章站
2023-12-14 09:15:10
核心代码:var time_range = function (begintime, endtime, nowtime) { var strb = begintime.split (":"); i...
核心代码:
var time_range = function (begintime, endtime, nowtime) { var strb = begintime.split (":"); if (strb.length != 2) { return false; } var stre = endtime.split (":"); if (stre.length != 2) { return false; } var strn = nowtime.split (":"); if (stre.length != 2) { return false; } var b = new date (); var e = new date (); var n = new date (); b.sethours (strb[0]); b.setminutes (strb[1]); e.sethours (stre[0]); e.setminutes (stre[1]); n.sethours (strn[0]); n.setminutes (strn[1]); if (n.gettime () - b.gettime () > 0 && n.gettime () - e.gettime () < 0) { return true; } else { alert ("当前时间是:" + n.gethours () + ":" + n.getminutes () + ",不在该时间范围内!"); return false; } } time_range ("21:30", "23:30", "3:22");
效果:
当前时间的情况下:
var time_range = function (begintime, endtime) { var strb = begintime.split (":"); if (strb.length != 2) { return false; } var stre = endtime.split (":"); if (stre.length != 2) { return false; } var b = new date (); var e = new date (); var n = new date (); b.sethours (strb[0]); b.setminutes (strb[1]); e.sethours (stre[0]); e.setminutes (stre[1]); if (n.gettime () - b.gettime () > 0 && n.gettime () - e.gettime () < 0) { return true; } else { alert ("当前时间是:" + n.gethours () + ":" + n.getminutes () + ",不在该时间范围内!"); return false; } } time_range ("21:30", "23:30");