Home > Article > Web Front-end > JavaScript Advanced Programming Reading Notes No. 10 Local Object Date_Javascript Skills
Create
var d=new Date();
Note that in JavaScript the month value is from 0 to 11 (0 means January).
Set date and time values
There are two ways to set date and time values:
1. Only declare the number of milliseconds from 12:00 a.m. on January 1, 1970
a. Directly use the number of milliseconds from 12 a.m. on January 1, 1970
var d=new Date(0);
b. parse method:
parse The method accepts a string as a parameter, converts the string into a date value, and returns the number of milliseconds.
For example, create a Date object for February 27, 2012:
var d=new Date(Date.parse("Feb 27,2012"));
If passed to parse The string of the method cannot be converted into a date. The function returns NaN
c. UTC method:
The UTC method also returns the millisecond representation of the date, but the parameters are year, month, day, hour, Minutes, seconds, milliseconds, year and month are required, others are optional.
For example, create a Date object for February 27, 2012:
var d=new Date(Date.UTC(2012,1,27));
2. Directly declare UTC Parameters accepted by the method
var d=new Date(2012,1,27);
The parameter rules are the same as the UTC method.
Date class method
Date class method is as follows (from: http://www.jb51.net/w3school/js/jsref_obj_date.htm):
Method | Description | FF | IE |
---|---|---|---|
Date() | Returns the date and time of the current day. | 1 | 3 |
getDate() | Returns the day of the month (1 ~ 31) from the Date object. | 1 | 3 |
getDay() | Returns the day of the week (0 ~ 6) from a Date object. | 1 | 3 |
getMonth() | Returns the month (0 ~ 11) from the Date object. | 1 | 3 |
getFullYear() | Returns the year as a four-digit number from a Date object. | 1 | 4 |
getYear() | Please use getFullYear() method instead. | 1 | 3 |
getHours() | Returns the hour of the Date object (0 ~ 23). | 1 | 3 |
getMinutes() | Returns the minute (0 ~ 59) of the Date object. | 1 | 3 |
getSeconds() | Returns the number of seconds in a Date object (0 ~ 59). | 1 | 3 |
getMilliseconds() | Returns the millisecond (0 ~ 999) of the Date object. | 1 | 4 |
getTime() | Returns the number of milliseconds since January 1, 1970. | 1 | 3 |
getTimezoneOffset() | Returns the difference in minutes between local time and Greenwich Mean Time (GMT). | 1 | 3 |
getUTCDate() | Returns the day of the month (1 ~ 31) from a Date object based on universal time. | 1 | 4 |
getUTCDay() | Returns the day of the week (0 ~ 6) from a Date object based on universal time. | 1 | 4 |
getUTCMonth() | Returns the month (0 ~ 11) from the Date object according to universal time. | 1 | 4 |
getUTCFulYear() | Returns the four-digit year from a Date object based on universal time. | 1 | 4 |
getUTCHours() | Returns the hour of the Date object according to universal time (0 ~ 23). | 1 | 4 |
getUTCMinutes() | Returns the minute of the Date object according to universal time (0 ~ 59). | 1 | 4 |
getUTCSeconds() | Returns the seconds (0 ~ 59) of the Date object according to universal time. | 1 | 4 |
getUTCMilliseconds() | Returns milliseconds (0 ~ 999) of a Date object according to universal time. | 1 | 4 |
parse() | Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). | 1 | 3 |
setDate() | Set the day of the month (1 ~ 31) in the Date object. | 1 | 3 |
setMonth() | Set the month (0 ~ 11) in the Date object. | 1 | 3 |
setFullYear() | Set the year (four digits) in the Date object. | 1 | 4 |
setYear() | Please use the setFullYear() method instead. | 1 | 3 |
setHours() | Set the hour (0 ~ 23) in the Date object. | 1 | 3 |
setMinutes() | Set the minute (0 ~ 59) in the Date object. | 1 | 3 |
setSeconds() | Sets the seconds in the Date object (0 ~ 59). | 1 | 3 |
setMilliseconds() | Set the milliseconds (0 ~ 999) in the Date object. | 1 | 4 |
setTime() | Sets the Date object in milliseconds. | 1 | 3 |
setUTCDate() | Set the day of the month in the Date object (1 ~ 31) according to universal time. | 1 | 4 |
setUTCMonth() | Sets the month (0 ~ 11) in the Date object according to universal time. | 1 | 4 |
setUTCFulYear() | Sets the year (four digits) in the Date object according to universal time. | 1 | 4 |
setUTCHours() | Sets the hour in the Date object according to universal time (0 ~ 23). | 1 | 4 |
setUTCMinutes() | Set the minute in the Date object according to universal time (0 ~ 59). | 1 | 4 |
setUTCSeconds() | Sets the seconds in the Date object (0 ~ 59) according to universal time. | 1 | 4 |
setUTCMilliseconds() | Sets the milliseconds in the Date object according to universal time (0 ~ 999). | 1 | 4 |
toSource() | Returns the source code of this object. | 1 | - |
toString() | Convert Date object to string. | 1 | 4 |
toTimeString() | Convert the time part of the Date object to a string. | 1 | 4 |
toDateString() | Convert the date part of the Date object to a string. | 1 | 4 |
toGMTString() | Please use toUTCString() method instead. | 1 | 3 |
toUTCString() | Convert Date object to string according to universal time. | 1 | 4 |
toLocaleString() | Convert Date object to string according to local time format. | 1 | 3 |
toLocaleTimeString() | Convert the time part of the Date object into a string according to the local time format. | 1 | 3 |
toLocaleDateString() | Convert the date part of the Date object into a string according to the local time format. | 1 | 3 |
UTC() | Returns the number of milliseconds between January 1, 1997 and the specified date according to universal time. | 1 | 3 |
valueOf() | Returns the original value of the Date object. | 1 | 4 |