Home > Web Front-end > JS Tutorial > js implementation code to determine whether the selected time (or current time) is within a certain time period_time and date

js implementation code to determine whether the selected time (or current time) is within a certain time period_time and date

WBOY
Release: 2016-05-16 15:40:54
Original
951 people have browsed it

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 ("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",不在该时间范围内!");
    return false;
  }
}
time_range ("21:30", "23:30", "3:22");
Copy after login

Effect:

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 ("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",不在该时间范围内!");
    return false;
  }
}
time_range ("21:30", "23:30");
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template