javascript regex iso datetime

前端 未结 9 1978
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 16:31

does anyone have a good regex pattern for matching iso datetimes?

ie: 2010-06-15T00:00:00

9条回答
  •  被撕碎了的回忆
    2020-12-02 17:05

    yyyy-MM-dd

    Too much explanation for most of the answers here, here's a short variation of @Sergey answer addressing some weird scenarios (like 2020-00-00), this RegExp only cares about the yyyy-MM-dd date:

    // yyyy-MM-dd
    ^\d{4}-([0][1-9]|1[0-2])-([0-2][1-9]|[1-3]0|3[01])$
    

    Also this one doesn't care about the number of days per month, like 2020-11-31 (because November has only 30 days).

    My use-case was to convert a String into a Date (from an API param) and I needed only to know that the input string didn't contained strange stuff, I do the next validation against an actual Date object.

提交回复
热议问题