Shane
Shane

Reputation: 5677

12 AM is considered as next day

12 AM is considered as next day

timeone = "5/18/2017 01:00 AM"
currenTime = "5/18/2017 05:55 AM"
timetwo = "5/18/2017  12:00 AM"

I see that this condition fails when the time is 12:00 AM, how do i handle this case?

timeone.isBefore(currentTime) => passes
currentTime.isBefore(timetwo) => fails

if (timeone.isBefore(currentTime) && currentTime.isBefore(timetwo)) {

}

Update:

var timeone = moment(time1, 'MM/DD/YYYY hh:mm a');
var timetwo = moment(time2, 'MM/DD/YYYY hh:mm a');
var currentTime = moment(currentTime, 'MM/DD/YYYY hh:mm a');

Upvotes: 6

Views: 7049

Answers (2)

IMSoP
IMSoP

Reputation: 97688

"12:00 AM" and "12:00 PM" are technically ambiguous, because AM means "before noon", and PM "after noon". Since noon is neither before nor after noon, and midnight could be considered the end of one day or the start of the next, neither label fits.

However, it's a very common interpretation that 12:00AM means midnight (00:00 in 24-hour notation) and 12:00PM means noon (12:00 in 24-hour notation). Wikipedia has a section on various interpretations.

This is the interpretation your library is using, so "5/18/2017 12:00 AM" = "5/18/2017 00:00", which is clearly before "5/18/2017 05:55 AM".

If your users are consistently expecting the opposite interpretation, you could pre-process the input to swap values around. However, it might be safer to fail validation and force them to enter "12:00 noon", or even reject "12:00" completely and make them enter "12:01 PM", which is (I think) unambiguous.

Upvotes: 8

Jamiec
Jamiec

Reputation: 136074

currenTime = "5/18/2017 05:55 AM"
timetwo = "5/18/2017 12:00 AM"
currentTime.isBefore(timetwo) => fails

When you say "fails" you means returns false - and this is expected. 5:55am is not before midnight as midnight is the start of the day, not the end of the day.

Upvotes: 2

Related Questions