2

I am trying programmatically check if the current time for my web application is between two predefined times. I don't know how to get the current time and then set variables that store two predefined times. The pseudo code would look like this

if(currentTime < shiftStart && currentTime <shiftEnd)
{
   shift = 2
}

But how do I declare and setup the variables so the code works correctly.

4
  • How are defined your times ? Commented Aug 8, 2013 at 17:34
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 8, 2013 at 17:35
  • 1
    Shouldn't the first comparison be using > instead of <? Commented Aug 8, 2013 at 17:38
  • Use simple numerical timestamps. Commented Aug 8, 2013 at 17:43

2 Answers 2

5
var now = Date.now();
var time1 = //whatever time1 is
var time2 = //whatever time2 is

if (now > time1 && now < time2)
{
    alert("blah"):
}
Sign up to request clarification or add additional context in comments.

Comments

4

var diff = date.getTime() - (new Date()).getTime(); gives you the difference between the time in milliseconds.

var diff1=shiftStart.getTime() - (new Date()).getTime();
var diff2=shiftEnd.getTime() - (new Date()).getTime();

if(diff1 < 0 && diff2 > 0 ) 
{
    shift = 2;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.