The most detailed JS date in history regular expression sharing _ regular expression

Source: Internet
Author: User
Tags getdate iso 8601

Simplest regular example:/d{4}-/d{2}-/d{2}
But the reality is not so simple, to consider, effectiveness and leap year and other issues ...

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

The Unix timestamp is 0 in accordance with the ISO 8601 specification: 1970-01-01t00:00:00z.

Consider first three rules unrelated to the year, and the year can be unified writing

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

Only the regular of month and day is considered below

1. The month of all years, including excepting, contains 1-28 days

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

2. All years, including excepting, include 29 and 30th except February

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

3. All years, including excepting, 1, 3, 5, 7, 8, 10, December all included 31st

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

All other dates except February 29 in a 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 implementation of a leap year

1: Four Years a leap
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26])

2: Hundred years does not leap, 400 year again leap
(0[48]| [2468] [048]| [13579] [26]) 00

3: Together is the February 29 of all leap years
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26]) | (0[48]| [2468] [048]| [13579] [26]) 00)-02-29)

All four rules are implemented and have no effect on each other, and together is the regular of all dates that match 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-9]{2} (0[48]|[ 2468][048]| [13579] [26]) | (0[48]| [2468] [048]| [13579] [26]) 00)-02-29) $

Given that this regular expression is used only as validation, capturing groups are meaningless, consume resources and affect the efficiency of matching, so you can use a non-capturing group 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) $
Category: JavaScript


JS Date Regular Expressions Support month-day validation
This JS date regular expression support month-day validation, we will give an example to illustrate the date validation expression of the function instance.

function IsDate (str) {
var result=str.match (/^ (D{4}) (-|/) (d{1,2}) 2 (d{1,2}) $/);
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 for Yyyy-mm-dd | | Date format for YYYY/MM/DD


Face only the regular of month and day

1. The month of all years, including excepting, contains 1-28 days

(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])
2. All years, including excepting, include 29 and 30th except February

(0[13-9]|1[0-2])-(29|30)
3. All years, including excepting, 1, 3, 5, 7, 8, 10, December all included 31st

(0[13578]|1[02])-31)
All other dates except February 29 in a 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{1,2}) 2 (d{1,2}) (d{1,2}):(d{1,2}):(d{1,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 if 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.

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

This is Yyyy-mm-ddde.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.