javascript regex iso datetime

前端 未结 9 1975
没有蜡笔的小新
没有蜡笔的小新 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:02

    Here is a regular expression to check ISO 8601 date format including leap years and short-long months. To run this, you'll need to "ignore white-space". A compacted version without white-space is on regexlib: http://regexlib.com/REDetails.aspx?regexp_id=3344

    There's more to ISO 8601 - this regex only cares for dates, but you can easily extend it to support time validation which is not that tricky.

    Update: This works now with javascript (without lookbehinds)

      ^(?:
          (?=
                [02468][048]00
                |[13579][26]00
                |[0-9][0-9]0[48]
                |[0-9][0-9][2468][048]
                |[0-9][0-9][13579][26]              
          )
    
          \d{4}
    
          (?:
    
            (-|)
    
            (?:
    
                (?:
                    00[1-9]
                    |0[1-9][0-9]
                    |[1-2][0-9][0-9]
                    |3[0-5][0-9]
                    |36[0-6]
                )
                |
                    (?:01|03|05|07|08|10|12)
                    (?:
                      \1
                      (?:0[1-9]|[12][0-9]|3[01])
                    )?            
                |
                    (?:04|06|09|11)
                    (?:
                      \1
                      (?:0[1-9]|[12][0-9]|30)
                    )?            
                |
                    02
                    (?:
                      \1
                      (?:0[1-9]|[12][0-9])
                    )?
    
                |
                    W(?:0[1-9]|[1-4][0-9]|5[0-3])
                    (?:
                      \1
                      [1-7]
                    )?
    
            )            
          )?
      )$
      |
      ^(?:
          (?!
                [02468][048]00
                |[13579][26]00
                |[0-9][0-9]0[48]
                |[0-9][0-9][2468][048]
                |[0-9][0-9][13579][26]              
          )
    
          \d{4}
    
          (?:
    
            (-|)
    
            (?:
    
                (?:
                    00[1-9]
                    |0[1-9][0-9]
                    |[1-2][0-9][0-9]
                    |3[0-5][0-9]
                    |36[0-5]
                )
                |
                    (?:01|03|05|07|08|10|12)
                    (?:
                      \2
                      (?:0[1-9]|[12][0-9]|3[01])
                    )?
    
                |
                    (?:04|06|09|11)
                    (?:
                      \2
                      (?:0[1-9]|[12][0-9]|30)
                    )?
                |
                    (?:02)
                    (?:
                      \2
                      (?:0[1-9]|1[0-9]|2[0-8])
                    )?
                |
                    W(?:0[1-9]|[1-4][0-9]|5[0-3])
                    (?:
                      \2
                      [1-7]
                    )?
           ) 
        )?
    )$
    

    To cater for time, add something like this to the mixture (from: http://underground.infovark.com/2008/07/22/iso-date-validation-regex/ ):

    ([T\s](([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)?(\15([0-5]\d))?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?
    

提交回复
热议问题