0

I am validating the date and time with moment().isValid(), but the date and time validation at 24:00:00 does not work as expected.

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

I want the result of the above code to be false, but the actual behavior is true. How can I implement the above phenomenon to get the expected result?

We also conducted tests on parts that seemed to be related, but the behavior at 60 minutes, 60 seconds, etc. was as expected.

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

// other
console.log(moment('2020-07-02 24:00:01','YYYY-MM-DD HH:mm:ss',true).isValid()); //false
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

1 Answer 1

0

It seems caused by following issue:

In ISO 8601, Midnight is a special case and can be referred to as both "00:00" and "24:00" (Midnight day after and Midnight today so to speak). While it should not technically "bubble", it is perfectly valid.


As a supplement, current spec does not seem to permit it:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.