Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 865
Shane Avatar asked May 18 '17 12:05

Shane


People also ask

Is 12 am tonight or tomorrow?

Also, if you wonder whether tonight's midnight really belong to tonight – or to tomorrow morning: astronomers and the military use a system in which midnight is 0 hours. In that system, tonight's midnight is the first moment of tomorrow. But as for the rest of us – there's no official answer.


2 Answers

"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.

like image 59
IMSoP Avatar answered Sep 30 '22 23:09

IMSoP


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.

like image 44
Jamiec Avatar answered Sep 30 '22 22:09

Jamiec