0
if (userDate.getHours() >= sysDate.getHours()) {
     alert('continue');
} else {
     alert('time is up');
}

I need to compare the system time with user entered time.By the above method i am able to compare the hours but i also need to compare minutes.pls suggest a suitable method for this

keeplearning
  • 369
  • 2
  • 6
  • 17
  • 1
    what do you mean with "user entered time" ? Is there a field where the user can enter time? – Bart Friederichs Jan 02 '13 at 10:02
  • yes the time picked by user it is in 24hr format something like 10:00:00.Also i am picking a date from jsp page and concatenated the date and time using javascript. – keeplearning Jan 02 '13 at 10:10
  • 1
    Comparing minutes? Is that the desired granularity of the comparison? What about dates? If userDate has a value of 01.01.2013 00:01, and sysDate is 02.01.2013 23:59, your comparison would still succeed even though the time would be up by two minutes... – Lucero Jan 02 '13 at 10:10
  • no only if dates are equal,i need to compare the time – keeplearning Jan 02 '13 at 10:12

2 Answers2

1

If both userDate and sysDate are Date object, you can use getTime() method, like the following:

if (userDate.getTime() >= sysDate.getTime()) {
     alert('continue');
} else {
     alert('time is up');
}
ntalbs
  • 28,700
  • 8
  • 66
  • 83
0

As shown here: How do you get a timestamp in JavaScript?

you can use Date.getTime() to get milleseconds since Epoch. Then use simple integer math to compare:

if (userDate.getTime() >= sysDate.getTime()) {
     alert('continue');
} else {
     alert('time is up');
}
Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195