zoukankan      html  css  js  c++  java
  • js 判断所选时间(或者当前时间)是否在某一时间段

     1 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");
  • 相关阅读:
    Codeforces Global Round 6
    Codeforces Global Round 5
    笔记
    笔记
    Codeforces Round #608 (Div. 2)
    模板
    Codeforces Round #607 (Div. 2)
    find命令
    while循环脚本
    发邮件
  • 原文地址:https://www.cnblogs.com/vincent_ds/p/2683755.html
Copyright © 2011-2022 走看看