JD Isaacks JD Isaacks - 7 months ago 13
Javascript Question

How to tell if string time-A is before time-B?

if I have 4 variables

startTime;
endTime;
startMerid;
endMerid;

startMarid and endMarid are either going to be 'AM' or 'PM'.

but startTime and endTime are going to be strings like 'dd:dd'

so it may be start:

12:30 PM
and end:
5:30 PM


How can I validate that the end time is not before the start time?

Thanks!!

Answer

You can use DateJS:

Date.parse('12:30pm') < Date.parse('5:30pm'); // true

EDIT:

Justin has a good point below. You can find the latest en-US version here

You can find the others in /trunk/build

EDIT 2016:

For those hitting this post in 2016+, it looks like a more up to date repo for DateJS is now at GitHub: https://github.com/datejs/Datejs

Also, it seems MomentJS is a very popular choice these days.