简体   繁体   中英

How can I validate a date including “00:07 PM” in JavaScript?

Has someone validated date of this format

Feb 9, 2010 12:07 PM

in javascript previously.There is a problem with javascript Date object its accepting Feb 9,2010 00:07 PM as a valid date.

如果您喜欢使用库, date.js旨在处理这种情况。

Meridiem has its roots in Latin meaning noon or midday. So saying 00:07 PM, or in other words 7 minutes past noon is technically correct though not commonly used.

On the other hand, saying 12 PM is then incorrect as it means 12 hours past noon, or midnight.

So I guess it's more convention than anything, but 00:07 PM is indeed correct. Also, testing on Chrome correctly flags values over 12, such as 13:07 PM as an invalid date.

You might find this section on Confusion at noon and midnight in a 12 hour clock useful.

The behavior of the Date constructor is unspecified, so even if you can explain this behavior, you cannot rely on it working the same way across other browsers. If you want reliable behavior, you should use a library like datejs or dojo.

00 PM is noon. Seems fine to me. It won't accept any hours >12 when using AM/PM so it appears to be intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM