JS determines whether the selected time (or current time) is in a time period of the implementation code _ date

Source: Internet
Author: User
Tags current time

Core code:

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 ("Current time is:" + n.gethours () + ":" + n.getminutes () + ", not within that time range!" ");
    return false;
  }
}
Time_range ("21:30", "23:30", "3:22");

Effect:

In the case of the current time:

 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 ("Current time is:" + n.gethours () + ":" + n.getminutes () + ", not within that time range!
    ");
  return false; 
} time_range ("21:30", "23:30"); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.