Reputation: 255
the following code is working fine with any start time and end time but when i give the end time 12 am or 12 pm the following code not giving the correct output.
var startTime = $("#txtStartTime").val(); eg. 7:00 PM
var from = new Date("November 13, 2013 " + startTime);
from = from.getTime(); eg. 1384349400000
var endTime = $("#txtEndTime").val(); eg. 12:00 AM or 12:00 PM
var to = new Date("November 13, 2013 " + endTime);
to = to.getTime(); eg. 1384281000000
if (from > to || from == to)
{
html += "<li>Start-time must be smaller than End-time</li>";
}
for example : when i give start time 7:00 PM and end time 12:00 AM or 12:00 PM it shows me Start-time must be smaller than End-time. Can you please tell me how can i fix it.
Thanks in advance.
Upvotes: 0
Views: 667
Reputation: 999
This might help you out:
// This represents: Thu Nov 13 2013 19:00:00 GMT-0500 (EST)
var date1 = new Date('November 13, 2013 07:00 PM');
// This represents: Thu Nov 13 2013 12:00:00 GMT-0500 (EST)
var date2 = new Date('November 13, 2013 12:00 PM');
// This represents: Thu Nov 13 2013 00:00:00 GMT-0500 (EST)
var date3 = new Date('November 13, 2013 12:00 AM'):
Therefore:
date1 > date2 > date3;
This might make more intuitive sense if you convert the numbers to their 24hour counterparts.
If you did,
12:00 AM becomes 00:00. 12:00 PM becomes 12:00. 7:00 PM becomes 19:00.
Therefore, 00:00 < 12:00 < 19:00 and 12:00 AM < 12:00 PM < 7:00 PM. So your from is always greater than your to in this situation.
In other words, it's working as intended :)
Upvotes: 1
Reputation: 881523
There is no 12am or 12pm (see here for example), the whole definition of those terms (ante and post meridian) means that it's neither am nor pm on the 12 o'clock time itself.
For the first case, it's indisputably correct. Whether 12am is mid-day on the 13th, or midnight between the 12th and 13th, 7pm is greater.
The second case you could argue either that 12pm represents midday on the 13th or midnight between the 13th and 14th. In the former case, it is again correct. The latter case not so much.
Best bet would probably to see what difference there is between 12pm and 12:01pm (either one minute or just shy of twelve hours). That will tell you how it's interpreting 12pm.
Upvotes: 3