Understanding the Behavior of Moment().isValid() at 24:00:00

Asked 1 years ago, Updated 1 years ago, 31 views

We are conducting a validation of the date and time in moment().isValid(), but the validation of the date and time at 24:00:00 does not work as expected.

//HH (=00~23) is a strict comparison, so true is not expected.
console.log( moment('2020-07-02 24:00', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // true

For my part, I would like the result of the above code to be false, but the actual behavior will be true.
How can I implement the above symptoms to achieve the expected results?

We also tested relevant areas, but the behavior of 60 minutes and 60 seconds was as expected.

//---------------------------------
//  behavior of 24:00:00
// ----------------------------

// be not a strict comparison and therefore unexpected
console.log(moment('2020-07-02 24:00').isValid());//true

// be not a strict comparison and therefore unexpected
console.log(moment('2020-07-02 24:00', 'YYYY-MM-DD HH:mm:ss') .isValid()); // true

// True is not expected because it is a strict comparison that assumes HH (=00~23)
console.log( moment('2020-07-02 24:00', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // true

// False is expected because it is a strict comparison that assumes hh (=01-12)
console.log( moment('2020-07-02 24:00', 'YYYY-MM-DD hh:mm:ss', true).isValid()); // false

// True is within expectations because it is a strict comparison that assumes kk (=01-24).
console.log( moment('2020-07-02 24:00', 'YYYY-MM-DD kk:mm:ss', true).isValid()); // true

// ----------------------------
//  Verifying Other Falsees
// ----------------------------
console.log(moment('2020-07-02 24:00:01', 'YYYY-MM-DD HH:mm:ss', true).isValid()); //
console.log( moment('2020-07-02 24:01:00', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // false
console.log( moment('2020-07-02 23:58:60', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // false
console.log( moment('2020-07-02 23:59:60', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // false
console.log( moment('2020-07-02 22:60:00', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // false
console.log( moment('2020-07-02 23:60:00', 'YYYY-MM-DD HH:mm:ss', true).isValid()); // false
console.log(moment('2020-07-02 23:60:60', 'YYYY-MM-DD HH:mm:ss', true).isValid());//false
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>

I use the 2.27.0 of moment.

javascript

2022-09-30 19:45

2 Answers

According to , ISO 8601 states that 24:00(:00) is appropriate as a midnight expression, so the behavior seems to be as described in the questionnaire.

The change difference at that time seems to be here, but at least at this point, it seems impossible to change behavior depending on some configuration.
(Note: It looks the same with the current latest version of the code)

Supplement:

However, the above issue is from 2013, but the current Wikpedia description has been updated as follows, and the latest (revised 2019?) specification does not allow 24:00.

Midnight is a special case and may be referred to as "00:00" or "24:00", except in ISO 8601-1:2019 where "24:00" is no longer permitted.


2022-09-30 19:45

The specification is in the official document, so it is inevitable that the validity is true and the decision will be made the next day.
When I searched the web, I couldn't find a way to make the behavior as expected, and the proposal to divide the string by / or was just a hit.(Forgotten link)

Excerpt from official documentation

2013-02-08 24:00:00.000 #hour24, minute, second, millisecond equal 0 means next day at midnight

In fact, 24:00 is the next day's decision, and 24:00:01 is the wrong format decision, which is strange.
However, we could not confirm the reason for this specification.

//true
console.log(moment('2020-07-02 24:00', 'YYYY-MM-DD HH:mm:ss', true).isValid());

// 2020-07-03 00:00:00 (decided next day at midnight)
console.log( moment('2020-07-02 24:00', 'YYYY-MM-DD HH:mm:ss', true).format('YYYY-MM-DD HH:mm:ss'));

// false (incorrect format with 1 second increase)
console.log(moment('2020-07-02 24:00:01', 'YYYY-MM-DD HH:mm:ss', true).isValid());


2022-09-30 19:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.