Moment: 00:00:00 should not be equal to 12:00:00 when parsed with h:m:s format

Created on 6 Jan 2012  ·  4Comments  ·  Source: moment/moment

These seem to be the wrong results - am I doing something wrong?

moment('12:00:00', 'hh:mm:ss').format('hh:mm:ss'); // "12:00:00"
moment('00:00:00', 'hh:mm:ss').format('hh:mm:ss'); // "12:00:00" (seems to be incorrect)

moment('12:30:00', 'hh:mm:ss').format('hh:mm:ss'); // "12:30:00"
moment('00:30:00', 'hh:mm:ss').format('hh:mm:ss'); // "12:30:00" (seems to be incorrect)

Most helpful comment

You should be using HH:mm:ss instead of hh:mm:ss in order to get a 24 hour clock.

However, there does seem to be a bug. 0 o'clock is not a valid hour in a 12-hour clock. I'm not sure what the desired behavior is in this situation though. It doesn't seem like you should get an error if it is 0 o'clock.

All 4 comments

You should be using HH:mm:ss instead of hh:mm:ss in order to get a 24 hour clock.

However, there does seem to be a bug. 0 o'clock is not a valid hour in a 12-hour clock. I'm not sure what the desired behavior is in this situation though. It doesn't seem like you should get an error if it is 0 o'clock.

Cool, thanks for the reply. I switched over to using HH but seem to still be getting the issue, at least with Chrome:

moment('12:00:00', 'HH:mm:ss').format('HH:mm:ss'); // "00:00:00" (incorrect?)
moment('00:00:00', 'HH:mm:ss').format('HH:mm:ss'); // "00:00:00" (correct behavior)

By default, HH and hh parse hours in 24 hour time. In order to parse in 12 hour time, you need to add 'am' or 'pm'.

The original post was be having correctly. You can see this if you add a to the formatting string.

moment('12:00:00', 'hh:mm:ss').format('hh:mm:ss a'); // "12:00:00 pm"
moment('00:00:00', 'hh:mm:ss').format('hh:mm:ss a'); // "12:00:00 am"

moment('12:30:00', 'hh:mm:ss').format('hh:mm:ss a'); // "12:30:00 pm"
moment('00:30:00', 'hh:mm:ss').format('hh:mm:ss a'); // "12:30:00 am"

I believe the issue below is a known issue that was fixed. It was caused by the commit in #74 and fixed in #92. I'm adding unit tests for it though. I'll also clarify the docs.

moment('12:00:00', 'HH:mm:ss').format('HH:mm:ss'); // "00:00:00"

If you are using 1.2.0, you should upgrade to 1.3.0, as that was when the bugfix was introduced.

Cool, sounds good. Thanks!

Was this page helpful?
0 / 5 - 0 ratings