Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the regular expressions for js dates

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what js date regular expressions have, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

The simplest rules are: / d {4}-/ d {2}-/ d {2}

But the actual situation is not that simple, to consider the effectiveness and leap years and so on.

For the valid range of dates, different application scenarios will vary. The valid range for DateTime objects defined in MSDN is: 0001-01-01 00:00:00 to 9999-12-31 23:59:59.

The 0 of the UNIX timestamp is 1970-01-01T00:00:00Z according to the ISO 8601 specification.

First consider the first three rules that have nothing to do with the year, and the year can be written in a unified way.

(?! 0000) [0-9] {4}

Consider only the regularity of the month and day below

1. The months of all years, including ordinary years, include 1-28 days.

(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8])

two。 All years, including normal years, include 29 and 30 except February.

(0 [13-9] | 1 [0-2])-(29 | 30)

3. All years 1, 3, 5, 7, 8, 10 and December, including normal years, include 31.

(0 [13578] | 1 [02])-31)

It adds up to all the dates except February 29th in the leap year.

(?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31)

Next, consider the realization of leap years.

1: a leap every four years

([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])

2: no leap in a hundred years, another leap in four hundred years

(0 [48] | [2468] [048] | [13579] [26]) 00

3: it adds up to February 29th of all leap years

([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [13579] [26]) 00)-02-29)

The four rules have been implemented and have no influence on each other. Together, they are the regularities of all dates that fall within the DateTime range.

^ (?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31) | (0 [0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48]) ] | [2468] [048] | [13579] [26]) 00)-02-29) $

Considering that this regular expression is only used for validation, the capture group is meaningless and will only take up resources and affect the matching efficiency, so you can use non-capture groups for optimization.

^ (?: (! 0000) [0-9] {4}-(: 0 [1-9] | 1 [0-2])-(?: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(?: 29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2} (?: 0 [48]) | 2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $

Classification: JavaScript

Js date regular expression supports month-day validation

This js date regular expression supports month-day verification. We will illustrate the function examples of date-validation expressions one by one.

Function isdate (str) {

Var result=str.match (/ ^ (d {4}) (- | /) (d {1Magin2}) 2 (d {1jin2}) $/)

If (result==null) return false

Var d=new Date (result [1], result [3]-1, result [4])

Return (d.getFullYear () = = result [1] & & d.getMonth () + 1==result [3] & & d.getDate () = = result [4])

}

Check whether it is YYYY-MM-DD | | date format of YYYY/MM/DD

The face only considers the regularity of the month and day.

1. The months of all years, including ordinary years, include 1-28 days.

(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8])

two。 All years, including normal years, include 29 and 30 except February.

(0 [13-9] | 1 [0-2])-(29 | 30)

3. All years 1, 3, 5, 7, 8, 10 and December, including normal years, include 31.

(0 [13578] | 1 [02])-31)

It adds up to all the dates except February 29th in the leap year.

(?! 0000) [0-9] {4}-(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8]) | (0 [13-9] | 1 [0-2])-(29 | 30) | (0 [13578] | 1 [02])-31)

Function isdatetime (str)

{

Var result=str.match (/ ^ (d {4}) (- | /) (d {1rem 2}) 2 (d {1jue 2}) (d {1jue 2}): (d {1je 2}): (d {1jue 2}) $/)

If (result==null) return false

Var d = new Date (result [1], result [3]-1, result [4], result [5], result [6], result [7])

Return (d.getFullYear () = = result [1] & & (d.getMonth () + 1) = = result [3] & & d.getDate () = = result [4] & & d.getHours () = = result [5] & & d.getMinutes () = = result [6] & d.getSeconds () = = result [7])

}

/ / determine whether the input is a valid long date format-"YYYY-MM-DD HH:MM:SS" | | "YYYY/MM/DD HH:MM:SS"

This is yyyy-mm-dd hh:mm:ss 's.

/ ^ (d {4})-(d {2})-(d {2}) (d {2}): (d {2}): (d {2}) $/

This is yyyy-mm-ddde 's.

/ ^ (d {4})-(d {2})-(d {2}) $/

Thank you for reading this article carefully. I hope the article "what are the js date regular expressions" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report