2

Hi i want to put condition on time that only time between 9:00 AM to 5:00 PM has to access.

here is my javascript code for it.

 var time = document.getElementsByName('s_time')[0].value;

if user enter tie before 9:00 AM or after 5:00 PM it should give alert. Please help me

User enterd time by using jquery picker.

user3432450
  • 21
  • 1
  • 3
  • How does the user enter time ? Is this a simple textbox or is it a date input ? Or maybe a JQuery datepicker ? – Anwar Feb 25 '15 at 07:42
  • If I had to ask you, you better add this precision in your post in order to let us know how to quickly solve your issue. – Anwar Feb 25 '15 at 07:50

2 Answers2

0

This is not complete solution, But hope this will help you

  • Get current time Get in milliseconds,
  • the time difference between
  • next execution time minus current time Settimeout with result millisecons

Here is simple example

You just need to calculate the difference between the current time and the target time and use setTimeout() with that value.

For example, depending on how your target browsers parse dates, you could do the following:

function alert3pm() {
  alert("It's 3PM!");
}
var timeAt3pm = new Date("1/31/2011 03:00:00 PM").getTime()
  , timeNow = new Date().getTime()
  , offsetMillis = timeAt3pm - timeNow;
setTimeout(alert3pm, offsetMillis);

Or rather, instaed of parsing a date (since that's really inconsistent between browsers) you could do something like this:

function getTimeAtHour(hour) {
  var t = new Date();
  t.setHours(hour);
  t.setMinutes(0);
  t.setSeconds(0);
  t.setMilliseconds(0);
  return t;
}
backtrack
  • 7,996
  • 5
  • 52
  • 99
0
var time = document.getElementsByName('s_time')[0].value || new Date().getTime();

var d = new Date(time);
var currentHour = d.getHours();
if (currentHour < 9 || currentHour >= 17) {
    alert('something');
}
Tune389
  • 91
  • 8