Common Date usage (Regular Expression and comparison) in javasrip)

Source: Internet
Author: User
Tags iso 8601

Date is the date and time creation function in js. Next I will introduce the usage of the date function, and then introduce in detail the example of date comparison and date Regular Expression in the application, I hope this article will bring you good luck.

The Date object is used to process the Date and time.
Syntax for creating a Date object:

The Code is as follows: Copy code
Var myDate = new Date ()

The Date object automatically saves the current Date and time as its initial value.
The following five parameters are supported:

The Code is as follows: Copy code

New Date ("month dd, yyyy hh: mm: ss ");
New Date ("month dd, yyyy ");
New Date (yyyy, mth, dd, hh, mm, ss );
New Date (yyyy, mth, dd );
New Date (MS );

Note that in the last form, the parameter represents the number of milliseconds between the creation time and the GMT time on January 1, January 1, 1970. The meanings of various functions are as follows:

Month: represents the name of a month in English, from January to December.

Mth: an integer representing the month, from (January 1, January) to 11 (January 1, December)

Dd: the day of a month, from 1 to 31.

Yyyy: The Year in four-digit format.

Hh: hours, from 0 (midnight) to 23)

Mm: The number of minutes, an integer from 0 to 59.

Ss: number of seconds, an integer from 0 to 59

Ms: Number of milliseconds, an integer greater than or equal to 0

For example:

The Code is as follows: Copy code

New Date ("January 12,2006 22:19:35 ");

New Date ("January 12,2006 ");

New Date );

New Date (2007,0, 12 );

New Date (1137075575000 );

Below we have sorted out some date Regular Expression


Simple regular expressions such as: d {4}-d {2}-d {2}
However, the actual situation is not that simple. You need to consider the issues such as validity and leap year .....

The effective range of dates varies in different application scenarios. The valid range of the DateTime object defined in MSDN is 0001-01-01 00:00:00 to 9999-12-31 23:59:59.

The UNIX timestamp 0 follows the ISO 8601 standard: 1970-01-01T00: 00: 00Z.

First, consider the first three rules irrelevant to the year. The year can be written in a unified manner.

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

Here we only consider the monthly and daily regular expressions.

1. The month of all years, including the normal year, contains 1-28 days.

The Code is as follows: Copy code

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

2. All the years, including the Year of the Year, including the year of the first month, include the 29 and 30 days.

The Code is as follows: Copy code

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

3. All years, including the Year of the Year, including the year 1, 3, 5, 7, 8, 10, and the year of the month, all contain the 31

The Code is as follows: Copy code

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

In combination, it is all the dates except the February 29 of the leap year.

The Code is as follows: Copy code

(?! 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 we will consider the implementation of the leap year.

1: four-year-old

The Code is as follows: Copy code
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])

2: Never renew for four hundred years, and never renew for years

The Code is as follows: Copy code
(0 [48] | [2468] [048] | [13579] [26]) 00

3: Combined is the year February 29 of all leap years

The Code is as follows: Copy code
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [13579] [26]) 00)-02-29)

All the four rules have been implemented and have no influence on each other. The combination is the regular expression of all dates that conform to the DateTime range.

The Code is as follows: Copy code

^ ((?! 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) $

Considering that this regular expression is only used for verification, it is meaningless to capture a group. It only occupies resources and affects matching efficiency. Therefore, you can use a non-capture group for optimization.

The Code is as follows: Copy code

^ (? :(?! (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 [1, 13578] | 1 [02])-31) | (? : [0-9] {2 }(? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $


The Date function compares the Date size, which is used in regular expressions.

The Code is as follows: Copy code

// Js regular expression to verify the Date format. Use the Date function to compare the Date size.
Var dateOfArrival = '2017-04-08 ';
Var year = '20140901 ';
Var month = '04 ';
Var day = '07 ';

/* Js Regular Expression verification date format */
Reg =/^ (? :(?! (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 [1, 13578] | 1 [02])-31) | (? : [0-9] {2 }(? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $ /;

If (! Reg. test (dateOfArrival )){
Alert ('date input is incorrect! ');
}
Else
{

// Date: Compare the Date size
Var today = new Date ();
Var arrivalDay = new Date (year, month-1, day );

If (arrivalDay <today)
{
Alert ('arrival date must be later than today's date! ');
}

}

The last part is the return value function of the date function.


Date () returns the Date and time of the current day.
GetDate () returns a day (1 ~ 31 ).
GetDay () returns a day of a week from the Date object (0 ~ 6 ).
GetMonth () returns the month from the Date object (0 ~ 11 ).
GetFullYear () returns the year from the Date object in four digits.
Use the getFullYear () method instead of getYear.
GetHours () returns the hour of the Date object (0 ~ 23 ).
GetMinutes () returns the minute of the Date object (0 ~ 59 ).
GetSeconds () returns the number of seconds of the Date object (0 ~ 59 ).
GetMilliseconds () returns the millisecond (0 ~) of the Date object ~ 999 ).
GetTime () returns the number of milliseconds since January 1, January 1, 1970.
GetTimezoneOffset () returns the minute difference between the local time and Greenwich Mean Time (GMT.
GetUTCDate () returns the Day (1 ~ 31 ).
GetUTCDay () returns the day of the week (0 ~ 6 ).
GetUTCMonth () returns the month (0 ~ 11 ).
GetUTCFullYear () returns the four-digit year from the Date object based on the universal time.
GetUTCHours () returns the hour of the Date object based on the Universal Time (0 ~ 23 ).
GetUTCMinutes () returns the minute of the Date object based on the Universal Time (0 ~ 59 ).
GetUTCSeconds () returns the second of the Date object based on the Universal Time (0 ~ 59 ).
GetUTCMilliseconds () returns the millisecond (0 ~ 999 ).
Parse () returns the number of milliseconds from midnight, January 1, January 1, 1970 to the specified date (string.
SetDate () sets a day of the month in the Date object (1 ~ 31 ).
SetMonth () sets the month (0 ~) in the Date object ~ 11 ).
SetFullYear () sets the year (four digits) in the Date object ).
Use the setFullYear () method instead of setYear.
SetHours () sets the hour (0 ~) in the Date object ~ 23 ).
SetMinutes () sets the minute (0 ~ 59 ).
SetSeconds () sets the second (0 ~) in the Date object ~ 59 ).
SetMilliseconds () sets the millisecond (0 ~) in the Date object ~ 999 ).
SetTime () sets the Date object in milliseconds.
SetUTCDate () sets the day of the month in the Date object based on the Universal Time (1 ~ 31 ).
SetUTCMonth () sets the month (0 ~ 11 ).
SetUTCFullYear () sets the year (four digits) in the Date object based on the universal time ).
SetUTCHours () sets the hour (0 ~ 23 ).
SetUTCMinutes () sets the minute (0 ~ 59 ).
SetUTCSeconds () sets the second (0 ~) in the Date object based on the universal time ~ 59 ).
SetUTCMilliseconds () sets the millisecond (0 ~ 999 ).
ToSource () returns the source code of the object.
ToString () converts a Date object to a string.
ToTimeString () converts the time part of the Date object to a string.
ToDateString () converts the Date part of the Date object to a string.
Use the toUTCString () method instead of toGMTString. 1 3
ToUTCString () converts a Date object to a string based on the universal time.
ToLocaleString () converts a Date object to a string based on the local time format.
ToLocaleTimeString () converts the time part of the Date object to a string based on the local time format.
ToLocaleDateString () converts the Date part of the Date object to a string based on the local time format.
UTC () returns the number of milliseconds from January 1, January 1, 1997 to the specified date based on the universal time.
ValueOf () returns the original value of the Date object.
Var objDate = new Date ([arguments list]);

Related Article

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.