The attributes and methods of the date object of the JavaScript native object _ Basic knowledge

Source: Internet
Author: User
Tags gmt time zone getdate local time string format

To create the syntax for a Date object:

Copy Code code as follows:

The Date object automatically saves the current date and time as its initial value.
New Date ();

value-millisecond: Represents the value that began 00:00:00 January 1, 1970, from world coordination.
New Date (value);

datestring-Date String: A string value that represents a date. This string should be the format recognized in the Parse method.
New Date (datestring);

Year-year: An integer value representing the year. To avoid the 2000 problem, it would be preferable to designate a 4-digit year; Use 1998 instead of 98
Month-Month: An integer value representing the month from 0 (January) to 11 (December)
day-Day: An integer value representing the day ordinal of one months, starting at 1
hour-hours: An integer value representing the number of hours in a day (24-hour system)
minute-minutes
second-seconds
Millisecond-ms
New Date (year, month, day [, hour, minute, second, millisecond]);

Date ()

The date () method returns the day and time.

Copy Code code as follows:

Console.log (Date ()); "Tue Sep 2013 12:22:55 gmt+0800 (China Standard Time)"

Parse ()

The parse () method resolves a date-time string and returns the number of milliseconds from the 1970/1/1 midnight distance from that date time.

Date.parse (datestring)

Parameter datestring is required to represent a string of dates and times.

Note that this method is a static method of the Date object. This method is typically invoked in the form of date.parse () rather than by Dateobject.parse ().

Copy Code code as follows:

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

UTC ()

The UTC () method returns the number of milliseconds from January 1, 1970 to a specified date based on the universal.

DATE.UTC (year, month, day, hours, minutes, seconds, MS)

The argument year is required to represent the four-digit number of years; month is required, the integer representing the month, between 0 ~ 11;day is optional, the integer representing the date, between 1 ~ 31;hours is optional, the integer representing the hour, between 0 ~ 23;minutes is optional, An integer that represents minutes, between 0 ~ 59;seconds is optional, an integer representing seconds, between 0 ~ 59;ms is optional, an integer representing milliseconds, between 0 ~ 999.

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

The date type in ECMAScript is built on the Java.util.Date class in early java. To do this, the date type uses the number of milliseconds from UTC (Coordinated Universal time, international coordination times) to start at midnight (0 o'clock) of January 1, 1970 to save dates. Under the condition that this data storage format is used, date types can be saved in exactly 285,616 years before or after January 1, 1970.

Note: The parse () date and time are created based on the local time zone rather than GMT. and UTC () is created based on GMT. Their parameters are not the same.

GMT: The standard Time of the world, that is, Greenwich location.

Copy Code code 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 a Data.now () method that returns the number of milliseconds that represent the date and time when this method was invoked. IE9 only began to support, but we can use the + operator to convert the data object to a string, to get the same value.

Copy Code code 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 bizarre, not only because of the different parameters of the interpretation results, and in the various browsers, the performance is different, as follows:

Copy Code code 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 00:00:00 gmt+0800 (China Standard Time)
Console.log (D2); Tue Mar 08:00:00 gmt+0800 (China Standard Time)
Console.log (D3); Tue Mar 00:00:00 gmt+0800 (China Standard Time)

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

To avoid these problems, please follow the following recommendations:

1. Insist on using the date string format "Yyyy/mm/dd"
2. Avoid using the date string format "Yyyy-mm-dd" with a hyphen
3. To specify a four-digit year
4.Chrome browsers can accept more date strings than other browsers, so if there is no problem with the Chrome browser, it does not mean that other browsers are fine.

For more information: JavaScript and dates, What a mess! and so in the discussion

Get series Methods

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. Be careful not to use getyear ().
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.

Set Series method

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. Be careful not to use the Setyear () method.
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.

ToString Series Method

ToString () Converts the Date object to a string, and ToString () always returns a string that is expressed in American English.
toTimeString () Converts the time portion of the Date object to a string.
toDateString () Converts the date part of a Date object to a string.
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.

Copy Code code as follows:

var d = new Date ();

Console.log (d); Tue Sep 2013 13:37:04 gmt+0800 (China Standard Time)
Console.log (D.tostring ()); Tue Sep 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, Sep 2013 05:37:04 GMT
Console.log (D.tolocalestring ()); September 17, 2013 1:37:04
Console.log (D.tolocaletimestring ()); 1:37:04
Console.log (D.tolocaledatestring ()); September 17, 2013

Note the toLocaleString () series method can receive parameters to determine what custom to output, reference: MDN

Copy Code code as follows:

var d = new Date ();

Console.log (d.tolocalestring ("Ko-kr")); 2013년9월17일오후1:48:24

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.