views:

67

answers:

2

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!!

+8  A: 

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

Matt
This is a great plugin, but be very careful what version of this plugin you are downloading. The front page still links to a version from Nov 2007, even though there is another version available from SVN that has about a year's worth of fixes. I found this out the hard way when I got burned by a bug where DateJS thought the `A` in "Aug 4, 2009" was for `AM`...
Justin Ethier
@Justin Ethier - thanks, I've been using the Nov 2007 version for years!
Matt
+2  A: 

Parse out the hours and minutes fields so you have two separate variables. Then add 12 to hours if it is PM, unless the hour value is already 12. You can then compare the values directly:

if (startTimeHrs * 60 + startTimeMins) > 
   (  endTimeHrs * 60 + endTimeMins){ ... }
Justin Ethier
`Then add 12 to hours if it is PM.` ruh-roh... you can't add 12 hours to the time if it's between 12pm and 12:59pm!
Andy E