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.

deadsix
  • 375
  • 1
  • 7
  • 25

2 Answers2

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

if (now > time1 && now < time2)
{
    alert("blah"):
}
Seano666
  • 2,238
  • 1
  • 15
  • 14
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;
}
Kira
  • 955
  • 1
  • 11
  • 25