开发者

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 开发者_JAVA技巧problem with javascript Date object its accepting Feb 9,2010 00:07 PM as a valid date.


If you fancy using a library, date.js is designed to handle this sort of situation.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜