Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the properties and methods of the Date object of JavaScript native objects_Basic knowledge

WBOY
Release: 2016-05-16 16:09:37
Original
1388 people have browsed it

Syntax for creating Date objects:

Copy code The code is as follows:

//The Date object will automatically save the current date and time to its initial values.
new Date();

//value-milliseconds: represents the value starting from 00:00:00 on January 1, 1970 UTC.
new Date(value);

//dateString-date string: a string value representing a date. This string should be in the format recognized in the parse method.
new Date(dateString);

//year-year: an integer value representing the year. To avoid year 2000 problems it is best to specify a 4-digit year; use 1998, not 98
//month-month: an integer value representing the month from 0 (January) to 11 (December)
//day-day: an 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 format)
//minute-minute
//second-second
//millisecond-millisecond
new Date(year, month, day [, hour, minute, second, millisecond]);

Date()

Date() method returns today’s date and time.

Copy code The code is as follows:

console.log(Date()); //"Tue Sep 17 2013 12:22:55 GMT 0800 (China Standard Time)"

parse()

The parse() method parses a datetime string and returns the number of milliseconds from midnight on January 1, 1970 to the datetime.

Date.parse(datestring)

The parameter datestring is required and represents a string of date and time.

Note that this method is a static method of the Date object. This method is generally called in the form of Date.parse() rather than through dateobject.parse().

Copy code The code is as follows:

console.log(Date.parse(Date())); //1379392301000
console.log(Date.parse("Aug 9, 1995")); //807897600000

UTC()

UTC() method returns the number of milliseconds from January 1, 1970 to the specified date according to universal time.

Date.UTC(year, month, day, hours, minutes, seconds, ms)

The parameter year is required, representing the four-digit number of the year; month is required, representing the integer of the month, ranging from 0 ~ 11; day is optional, representing the integer of the date, ranging from 1 ~ 31; hours is optional, representing an integer of hours, ranging from 0 ~ 23; minutes is optional, representing an integer of minutes, ranging from 0 ~ 59; seconds is optional, representing an integer of seconds, ranging from 0 ~ 59; ms is optional and represents an integer in milliseconds, ranging from 0 to 999.

Date.UTC() is a static method. The parameters of the Date.UTC() method specify the date and time, which are both UTC times, in the GMT time zone. The specified UTC time is converted to milliseconds so that it can be used by the Date() constructor and the Date.setTime() method.

The Date type in ECMAScript is built on the Java.util.Date class in early Java. For this purpose, the Date type uses the number of milliseconds that have elapsed since midnight (zero hour) on January 1, 1970, UTC (Coordinated Universal Time, International Coordinated Time) to save the date. Under the conditions of using this data storage format, the date saved by the Date type can be accurate to 285616 years before or after January 1, 1970.

Note: parse() dates and times are created based on the local time zone, not GMT. UTC() is created based on GMT. Their parameters are also different.

GMT: Universal Time, which is the standard time in Greenwich.

Copy code The code 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 is called. IE9 only started to support it, but we can use the operator to convert the Data object into a string and get the same value.

Copy code The code is as follows:

var d1 = Date.now();
var d2 = new Date();

console.log(d1); //1379393793104
console.log(d2); //1379393793104

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:

Copy code The code 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)

Performance reference in different browsers: http://dygraphs.com/date-formats.html

To avoid these problems, follow the advice below:

1. Stick to the date string format of “YYYY/MM/DD”
2. Avoid using the date string format "YYYY-MM-DD" with hyphens
3. Specify the four-digit year
4. Chrome browser can accept more date strings than other browsers, so if there is no problem in Chrome browser, it does not mean that there is no problem in other browsers

For more information, please refer to: JavaScript and Dates, What a Mess! and Discussions in SO

get series method

getDate() returns the day of the month (1 ~ 31) from the Date object.
getDay() returns the day of the week (0 ~ 6) from a Date object.
getMonth() returns the month (0 ~ 11) from a Date object.
getFullYear() Returns the year as a four-digit number from a Date object. Be careful not to use getYear().
getHours() returns the hours (0 ~ 23) of a Date object.
getMinutes() returns the minutes (0 ~ 59) of a Date object.
getSeconds() returns the number of seconds in a Date object (0 ~ 59).
getMilliseconds() returns the milliseconds (0 ~ 999) of the Date object.
getTime() returns the number of milliseconds since January 1, 1970.
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT).
getUTCDate() Returns the day of the month (1 ~ 31) from a Date object based on universal time.
getUTCDay() Returns the day of the week (0 ~ 6) from a Date object based on universal time.
getUTCMonth() returns the month (0 ~ 11) from a Date object based on universal time.
getUTCFulYear() Returns the four-digit year from a Date object based on universal time.
getUTCHours() returns the hour (0 ~ 23) of a Date object according to universal time.
getUTCMinutes() returns the minutes (0 ~ 59) of a Date object according to universal time.
getUTCSeconds() Returns the seconds (0 ~ 59) of a Date object according to universal time.
getUTCMilliseconds() returns the milliseconds (0 ~ 999) of a Date object according to universal time.

set series methods

setDate() sets the day of the month (1 ~ 31) in the Date object.
setMonth() sets the month (0 ~ 11) in the Date object.
setFullYear() Sets the year (four digits) in a 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 a Date object in milliseconds.
setUTCDate() Sets the day of the month (1 ~ 31) in the Date object according to universal time.
setUTCMonth() Sets the month (0 ~ 11) in the Date object according to universal time.
setUTCFulYear() Sets the year (four digits) in a Date object according to universal time.
setUTCHours() Sets the hour (0 ~ 23) in a Date object according to universal time.
setUTCMinutes() Sets the minutes in a Date object (0 ~ 59) according to universal time.
setUTCSeconds() Sets the seconds in a Date object (0 ~ 59) according to universal time.
setUTCMilliseconds() Sets the milliseconds in a Date object (0 ~ 999) according to universal time.

toString series methods

toString() Converts a Date object to a string. toString() always returns a string expressed in American English.
toTimeString() Converts the time portion of a Date object to a string.
toDateString() Converts the date portion of a Date object to a string.
toUTCString() Converts a Date object to a string according to universal time.
toLocaleString() Converts a Date object to a string according to the local time format.
toLocaleTimeString() Converts the time part of the Date object to a string according to the local time format.
toLocaleDateString() Converts the date part of the Date object to a string according to the local time format.

Copy code The code 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 1:37:04 pm
console.log(d.toLocaleTimeString()); //1:37:04 PM
console.log(d.toLocaleDateString()); //September 17, 2013

Note that the toLocaleString() series of methods can receive parameters to determine the output convention. Reference: MDN

Copy code The code is as follows:

var d = new Date();

console.log(d.toLocaleString("ko-KR")); //2013년 9월 17일 오후 1:48:24

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!