The Date object is used to process dates and times.
To create the syntax for 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 5 kinds of parameter form are:
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 the last form, which represents the number of milliseconds to create and the difference between GMT time January 1, 1970. The meanings of the various functions are as follows:
Month: Month name in English, from January to December
MTH: Month by integer, from (January) to 11 (December)
DD: Represents the day ordinal of one months, from 1 to 31
YYYY: four-digit year
HH: Hours, from 0 (midnight) to 23 (11 o'clock in the evening)
MM: Number of minutes, integers from 0 to 59
SS: Number of seconds, integers from 0 to 59
MS: Number of milliseconds, integer greater than or equal to 0
Such as:
The code is as follows |
Copy Code |
New Date ("January 12,2006 22:19:35"); New Date ("January 12,2006"); New Date (2006,0,12,22,19,35); New Date (2006,0,12); New Date (1137075575000); |
The following sorted out some of the date regular up
Simple regular like: 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
The code is as follows |
Copy Code |
(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
The code is as follows |
Copy Code |
(0[13-9]|1[0-2])-(29|30) |
3. All years, including excepting, 1, 3, 5, 7, 8, 10, December all included 31st
The code is as follows |
Copy Code |
(0[13578]|1[02])-31) |
All other dates except February 29 in a 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, consider the implementation of a leap year
1: Four Years a leap
The code is as follows |
Copy Code |
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26]) |
2: Hundred years does not leap, 400 year again leap
The code is as follows |
Copy Code |
(0[48]| [2468] [048]| [13579] [26]) 00 |
3: Together is the 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 four rules are implemented and have no effect on each other, and together is the regular of all dates that match 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) $ |
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.
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) $ |
The date function compares the size of the day and is used here for regular expressions.
The code is as follows |
Copy Code |
//js Regular validation date format, using the DATE function to compare the size of the day Var dateofarrival= ' 2013-04-08 '; var year= ' 2013 '; var month= ' 04 '; var day= ' 07 '; * JS Regular validation 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 [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 date size var today=new date (); var arrivalday=new Date (Year,month-1,day); if (arrivalday<today) { alert (' Arrival date is greater than today's date! '); } } |
Finally, attach some about Date function return value function
Date () Returns the day and time.
GetDate () returns one day of one months (1 ~ 31) from the Date object.
Getday () returns one day of the week from the Date object (0 ~ 6).
GetMonth () returns the month (0 ~ 11) from the Date object.
getFullYear () returns the year with a four-digit number from the Date object.
GetYear () Please use the getFullYear () method instead.
GetHours () returns the hour of the date object (0 ~ 23).
Getminutes () returns the minute (0 ~ 59) of the Date object.
Getseconds () returns the number of seconds (0 ~ 59) of the Date object.
Getmilliseconds () returns the milliseconds (0 ~ 999) of the Date object.
GetTime () returns the number of milliseconds since January 1, 1970.
getTimezoneOffset () returns the minute difference between local time and Greenwich Mean Time (GMT).
getUTCDate () Returns the day of the month from the Date object (1 ~ 31) according to the world.
getUTCDay () Returns the day of the week (0 ~ 6) from the Date object according to the world.
getUTCMonth () returns the month (0 ~ 11) from the Date object according to the world.
getUTCFullYear () returns a four-digit year from the Date object based on the world.
getUTCHours () returns the hour (0 ~ 23) of the date object according to the universal.
getUTCMinutes () returns the minute (0 ~ 59) of the date object according to the universal.
getUTCSeconds () returns the second (0 ~ 59) of the date object according to the universal.
getUTCMilliseconds () returns the millisecond (0 ~ 999) of the date object according to the universal.
Parse () returns the number of milliseconds from midnight January 1, 1970 to the specified date (string).
Setdate () Sets the day of the month in the Date object (1 ~ 31).
Setmonth () sets the month (0 ~ 11) in the Date object.
setFullYear () sets the year (four digits) in the Date object.
Setyear () Please use the setFullYear () method instead.
Sethours () Sets the hour in the Date object (0 ~ 23).
Setminutes () Sets the minutes in the Date object (0 ~ 59).
Setseconds () Sets the seconds in the Date object (0 ~ 59).
Setmilliseconds () Sets the milliseconds in the Date object (0 ~ 999).
SetTime () Sets the Date object in milliseconds.
setUTCDate () Sets the day of the month in the Date object according to the world (1 ~ 31).
setUTCMonth () sets the month (0 ~ 11) in the date object according to the world.
setUTCFullYear () sets the year (four digits) in the Date object based on the world.
setUTCHours () sets the hour (0 ~ 23) in the date object according to the world.
setUTCMinutes () sets the minute (0 ~ 59) of the date object according to the world.
setUTCSeconds () sets the second (0 ~ 59) of the Date object according to the world.
setUTCMilliseconds () sets the millisecond (0 ~ 999) in the Date object according to the world.
Tosource () returns the source code for the object.
ToString () Converts the Date object to a string.
toTimeString () Converts the time portion of the Date object to a string.
toDateString () Converts the date part of a Date object to a string.
toGMTString () Please use the toutcstring () method instead. 1 3
toUTCString () Converts a Date object to a string based on the universal.
toLocaleString () Converts the Date object to a string based on the local time format.
toLocaleTimeString () Converts the time portion of a Date object to a string, based on the local time format.
toLocaleDateString () Converts the date part of a Date object to a string, based on the local time format.
UTC () returns the number of milliseconds from January 1, 1997 to a specified date based on the universal.
ValueOf () returns the original value of the Date object.
var objdate=new Date ([arguments list]);