参考:http://www.cnblogs.com/vincent_ds/archive/2012/09/13/2683755.html
var time_range = function (beginTime, endTime, nowTime) { 2 var strb = beginTime.split (":"); 3 if (strb.length != 2) { 4 return false; 5 } 6 7 var stre = endTime.split (":"); 8 if (stre.length != 2) { 9 return false; 10 } 11 12 var strn = nowTime.split (":"); 13 if (stre.length != 2) { 14 return false; 15 } 16 var b = new Date (); 17 var e = new Date (); 18 var n = new Date (); 19 20 b.setHours (strb[0]); 21 b.setMinutes (strb[1]); 22 e.setHours (stre[0]); 23 e.setMinutes (stre[1]); 24 n.setHours (strn[0]); 25 n.setMinutes (strn[1]); 26 27 if (n.getTime () - b.getTime () > 0 && n.getTime () - e.getTime () < 0) { 28 return true; 29 } else { 30 alert ("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",不在该时间范围内!"); 31 return false; 32 } 33 } 34 time_range ("21:30", "23:30", "3:22");
判断当前时间是否在一个时间段内
1 var time_range = function (beginTime, endTime) { 2 var strb = beginTime.split (":"); 3 if (strb.length != 2) { 4 return false; 5 } 6 7 var stre = endTime.split (":"); 8 if (stre.length != 2) { 9 return false; 10 } 11 12 var b = new Date (); 13 var e = new Date (); 14 var n = new Date (); 15 16 b.setHours (strb[0]); 17 b.setMinutes (strb[1]); 18 e.setHours (stre[0]); 19 e.setMinutes (stre[1]); 20 21 if (n.getTime () - b.getTime () > 0 && n.getTime () - e.getTime () < 0) { 22 return true; 23 } else { 24 alert ("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",不在该时间范围内!"); 25 return false; 26 } 27 } 28 time_range ("21:30", "23:30");