Syntax for creating a Date object:
The code copy is as follows:
// The Date object will automatically save the current date and time as its initial value.
new Date();
//value-milliseconds: represents the value starting from 00:00:00 on January 1, 1970.
new Date(value);
//dateString-Date String: The string value representing the date. This string should be in the format recognized in the parse method.
new Date(dateString);
//year-year: represents the integer value of the year. To avoid the 2000 problem, it is best to specify a 4-digit year; use 1998 instead of 98
//month-month: The integer value representing the month from 0 (January) to 11 (December)
//day-day: The integer value representing the day of the month, starting from 1
//hour-hour: an integer value representing the number of hours in a day (24-hour system)
//minute-minute
//second-second-second
//millisecond-millisecond-millisecond
new Date(year, month, day [, hour, minute, second, millionsecond]);
Date()
The Date() method returns the date and time of the day.
The code copy is as follows:
console.log(Date()); //"Tue Sep 17 2013 12:22:55 GMT+0800 (China Standard Time)"
parse()
The parse() method parse a datetime string and returns the milliseconds from midnight to that datetime at 1970/1/1.
Date.parse(datestring)
The parameter datestring is required, a string representing the date and time.
Note that this method is a static method of the Date object. Generally, it is called in the form of Date.parse() instead of the method through dateobject.parse().
The code copy is as follows:
console.log(Date.parse(Date())); //1379392301000
console.log(Date.parse("Aug 9, 1995")); //807897600000
UTC()
The UTC() method can return the number of milliseconds from January 1, 1970 to the specified date based on world time.
Date.UTC(year, month, day, hours, minutes, seconds, ms)
The parameter year is required, representing four digits of the year; month is required, representing the integer of the month, between 0 and 11; day is optional, representing the integer of the date, between 1 and 31; hours is optional, representing the integer of the hour, between 0 and 23; minutes is optional, representing the integer of the minute, between 0 and 59; seconds is optional, representing the integer of the second, between 0 and 59; ms is optional, representing the integer of the millisecond, between 0 and 999.
Date.UTC() is a static method. The parameters of the Date.UTC() method specify the date and time, which are both UTC times and are in the GMT time zone. The specified UTC time is converted into milliseconds so that the constructor Date() and the method Date.setTime() can use it.
The Date type in ECMAScript is built on the basis of the Java.util.Date class in early Java. To do this, the Date type saves the date using the number of milliseconds elapsed from midnight (zero) on January 1, 1970. Under the conditions of using this data storage format, the date of the Date type can be saved to 285,616 before or after January 1, 1970.
Note: parse() date and time are created based on the local time zone rather than GMT. UTC() is created based on GMT. Their parameters are also different.
GMT: World Time, the standard time of Greenwich's location.
The code copy is as follows:
var d = new Date();
console.log(Date.parse(d)); //1379393562000
console.log(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds())); //1379422362020
ECMAScript5 adds the Data.now() method, which returns the number of milliseconds representing the date and time when this method was called. IE9 has just started to support it, but we can convert Data objects into strings through the + operator to get the same value.
The code copy is as follows:
var d1 = Date.now();
var d2 = + new Date();
console.log(d1); //1379393793104
console.log(d2); //1379393793104
The date conversion in JavaScript is very weird. Not only will there be different interpretation results due to different parameters, but the performance in each browser is also different, as follows:
The code copy is as follows:
var d1 = new Date("2012/03/13");
var d2 = new Date("2012-03-13");
var d3 = new Date("2012-3-13");
console.log(d1); //Tue Mar 13 2012 00:00:00 GMT+0800 (China Standard Time)
console.log(d2); //Tue Mar 13 2012 08:00:00 GMT+0800 (China Standard Time)
console.log(d3); //Tue Mar 13 2012 00:00:00 GMT+0800 (China Standard Time)
Reference for performance in different browsers: http://dygraphs.com/date-formats.html
To avoid these problems, please follow the following suggestions:
1. Stick to the date string format of "YYYY/MM/DD"
2. Avoid using date string format "YYYY-MM-DD" with hyphen
3. Specify a four-digit year
4. Chrome browsers can accept more date strings than other browsers, so if there is no problem with Chrome browser, it does not mean that other browsers have no problem.
For more information, please refer to: Discussions in JavaScript and Dates, What a Mess! and SO
Get series method
getDate() returns a day of the month from the Date object (1 ~ 31).
getDay() returns a day of the week from the Date object (0 ~ 6).
getMonth() returns month (0 ~ 11) from Date object.
getFullYear() returns the year as four digits from the Date object. Be careful not to use getYear().
getHours() Returns the hours (0 ~ 23) of the Date object.
getMinutes() Returns the minutes (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 from January 1, 1970 to the present.
getTimezoneOffset() returns the minute difference between local time and Greenwich Standard Time (GMT).
getUTCDate() returns a day in the month from the Date object according to the world time (1 ~ 31).
getUTCDay() returns the day of the week from the Date object according to the world time (0 ~ 6).
getUTCMonth() returns month (0 ~ 11) from Date object according to world time.
getUTCFullYear() returns a four-digit year from the Date object based on world time.
getUTCHours() returns the hour (0 ~ 23) of the Date object according to the world time.
getUTCMinutes() returns minutes of the Date object (0 ~ 59) according to world time.
getUTCSeconds() returns the second (0 ~ 59) of the Date object according to the world time.
getUTCMilliseconds() returns milliseconds of the Date object according to world time (0 ~ 999).
Set series method
setDate() Sets a certain 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. Be careful not to use the setYear() method.
setHours() Sets the hours (0 ~ 23) in the Date object.
setMinutes() Sets the minutes (0 ~ 59) in the Date object.
setSeconds() Sets the seconds (0 ~ 59) in the Date object.
setMilliseconds() Sets the milliseconds (0 ~ 999) in the Date object.
setTime() Sets the Date object in milliseconds.
setUTCDate() sets the day of the month in the Date object according to the world time (1 ~ 31).
setUTCMonth() sets the month (0 ~ 11) in the Date object according to the world time.
setUTCFullYear() Sets the year (four digits) in the Date object according to world time.
setUTCHours() sets the hours in the Date object according to the world time (0 ~ 23).
setUTCMinutes() sets the minutes in the Date object according to the world time (0 ~ 59).
setUTCSeconds() Sets the seconds in the Date object according to the world time (0 ~ 59).
setUTCMilliseconds() sets the milliseconds in the Date object according to the world time (0 ~ 999).
toString series methods
toString() converts the Date object into a string, and toString() always returns a string expressed in American English.
toTimeString() converts the time part of the Date object into a string.
toDateString() converts the date part of the Date object into a string.
toUTCString() converts the Date object into a string according to the world time.
toLocaleString() converts the Date object to a string according to the local time format.
toLocaleTimeString() converts the time part of the Date object into a string according to the local time format.
toLocaleDateString() converts the date part of the Date object into a string according to the local time format.
The code copy is as follows:
var d = new Date();
console.log(d); //Tue Sep 17 2013 13:37:04 GMT+0800 (China Standard Time)
console.log(d.toString()); //Tue Sep 17 2013 13:37:04 GMT+0800 (China Standard Time)
console.log(d.toTimeString()); //13:37:04 GMT+0800 (China Standard Time)
console.log(d.toDateString() ); //Tue Sep 17 2013
console.log(d.toUTCString()); //Tue, 17 Sep 2013 05:37:04 GMT
console.log(d.toLocaleString()); //September 17, 2013 at 1:37:04 pm
console.log(d.toLocaleTimeString()); //1:37:04 pm
console.log(d.toLocaleDateString()); //September 17, 2013
Note that the toLocaleString() series method can receive parameters to determine what habit to output, refer to: MDN
The code copy is as follows:
var d = new Date();
console.log(d.toLocaleString("ko-KR")); //2013년9월17일오후1:48:24