JS Basic Knowledge Review: Reference type (ii)

Source: Internet
Author: User
Tags date1 time zones iso 8601 month name string format

The date type in ECMAScript is built on the basis of the Java.util.Date class in early java.

Therefore, the date type uses the number of milliseconds since UTC (coordinated Universal time, international coordination hour) to start at 0 o'clock Midnight January 1, 1970 to save the dates.

With this data storage format, the date type can be saved for exactly 285,616 years from January 1, 1970 or later.

To create a Date object, use the new operator and the date constructor: Var now=new date ();

When you call the date constructor without passing parameters, the newly created object automatically obtains the current date and time.

If you want to create a Date object based on a specific date and time, you must pass in the number of milliseconds that represents that date.

To simplify this calculation process, ECMAScript provides two methods: Date.parse () and DATE.UTC ().

The Date.parse () method receives a string parameter that represents a date, and then attempts to return the number of milliseconds of the corresponding date based on the string.

ECMA-262 does not define which date format the Date.parse () should support, so the behavior of this method varies by implementation and usually varies by region.

Browsers that set the region to the United States generally accept the following date formats:

"Month/day/year", as in 6/13/2004

"English month tomorrow, year", such as January 12,2004

"English Day of the week English month name: minutes: Seconds time zone", such as: Tue May 2004 00:00:00 GMT-0700

ISO 8601 extended format yyyy-mm-ddthh:mm:ss.sssz, such as: 2004-05-25t00:00:00 (only compatible ECMASCRIPT5 implementations support this format)

If the string passed into the Date.parse () method cannot represent a date, it returns Nan.

In fact, if a string representing a date is passed directly to the date constructor, Date.parse () is also called in the background, for example: Var somedate=new Date ("may 25,2004");

Date objects and their implementations in different browsers have many strange behaviors, one of which is to say that the value of the out of range is replaced with the current value in order to generate the output.

For example, when parsing "January 32,2007", some browsers interpret it as "February 1,2007", while Opera prefers to insert the current date of the current month, returning "January current date, 2007".

The DATE.UTC () method also returns the number of milliseconds that represents a date, but it uses different information with date.parse () when building values.

The arguments for DATE.UTC () are the year, the month based on 0 (January is 0, February is 1, and so on), the Day of the month (1 to 31), the number of hours (0 to 23), the minute, the second, and the number of milliseconds.

Of these parameters, only the first two parameters are required, if the number of days in the month is not provided, the number of days is assumed to be 1, and if other parameters are omitted, all assumptions are 0.

var y2k=new Date (DATE.UTC (2000,0))//gmt time January 1, 2000 Midnight 0 O'Clock

var allfives=new Date (DATE.UTC (2005,4,5,17,55,55));//gmt time May 5, 2005 5:55:55

As with Date.parse (), the date constructor mimics Date.utc (), but one thing is distinctly different: the date and time are created based on the local time zone rather than GMT.

However, the date constructor receives the same parameters as DATE.UTC ().

var y2k=new Date (2000,0);//local time January 1, 2000 Midnight 0 O'Clock

var allfives=new Date (2005,4,5,17,55,55);//local time May 5, 2005 5:55:55

The addition of the Date.now () method in ECMAScript5 returns the number of milliseconds that represent the date and time when this method was called, simplifying the work of parsing the code with the Date object.

var start=date.now ();//Get Start time

DoSomething ();//Call function

var stop=date.now (), result=stop-start;//get stop Time

But browsers that support this approach are only ie9+, firefox3+, safari3+, Opera10.5, and Chrome.

In browsers that do not support it, using the + operator to convert a Date object to a string can also achieve the same purpose.

var start=+new date ();//Get Start time

DoSomething ();//Call function

var stop=+new date (), result=stop-start;//get stop Time

As with other reference types, the date type overrides the toLocaleString (), toString (), and valueof () methods, but the values returned by these methods are different from those in other types.

The toLocaleString () method of the date type returns the date and time in a format that is appropriate for the locale of the browser, which roughly means that the time format contains am or PM, but does not contain time zone information.

The ToString () method of the date type typically returns a time with time zone information and a date, where the time is typically expressed as military time (that is, the range of hours is 0 to 23).

The following lists the results of calling the toLocaleString () and ToString () methods in different browsers, output PST (PACIFIC standard Time, PST) February 1, 2007 0 o'clock Midnight:

-ie 8-

toLocaleString ()-thursday,february 01,2007 12:00:00 AM

ToString ()-thu Feb 1 00:00:00 PST 2007

-firefox 3.5-

toLocaleString ()-thursday,february 01,2007 12:00:00 AM

ToString ()-thu Feb 00:00:00 GMT-0800 (Pacific Standard Time)

-safari 4-

toLocaleString ()-thursday,february 01,2007 00:00:00

ToString ()-thu Feb 00:00:00 GMT-0800 (Pacific Standard Time)

-chrome 4-

toLocaleString ()-thu Feb 00:00:00 GMT-0800 (Pacific Standard Time)

ToString ()-thu Feb 00:00:00 GMT-0800 (Pacific Standard Time)

-opera 10-

toLocaleString () -2/1/2007 12:00:00 AM

ToString ()-thu,01 Feb 00:00:00 GMT-0800

The date and time formats that are returned in different browsers vary widely, but this difference is useful only when debugging code, which is not valuable when displaying dates and times.

The valueof () method of the date type does not return a string, but instead returns a millisecond representation of the date, so it is convenient to use comparison operators to compare date sizes:

var date1=new Date (2007,0,1);//"January 1,2007"

var date2=new Date (2007,1,1);//"February 1,2007"

alert (date1<date2);//true

alert (date1>date2);//false

The date type also has some specific ways to format dates as strings.

toDateString ()-Displays weekday, month, day, and year in an implementation-specific format;

toTimeString ()-Displays hours, minutes, seconds, and time zones in an implementation-specific format;

toLocaleDateString ()-Displays weekday, month, day, and year in an implementation-specific format;

toLocaleTimeString ()-Displays the time, minutes, and seconds in an implementation-specific format;

toUTCString ()-Displays the UTC date in a full format that is specific to the implementation.

As with the toLocaleString () method and the ToString () method, the output of these string format methods is different from the browser, so there is no method that can be used to display consistent date information in the user interface.

In addition to the method described earlier, there is a method called togmtstring (), which is an equivalent to toutcstring (), whose purpose is to ensure backward compatibility, but ECMAScript recommends that the code being written now be used toutcstring () method.

The UTC date refers to a date value that converts a date to GMT when there is no time zone bias.

The date type also defines some methods for directly acquiring and setting a specific part of a date value.

GetTime ()-Returns the number of milliseconds representing the date, the same as the value returned by the valueof () method

SetTime (MS)-set the date in milliseconds and change the entire date

getFullYear ()-year of four digits obtained

getUTCFullYear ()-Returns the four-digit year of the UTC date

setFullYear (year)-set the year of the date, the incoming year value must be a four-digit number

setUTCFullYear (year)-sets the year of the UTC date, the incoming year value must be a four-digit number

GetMonth ()-Returns the month of the date, where 0 represents January, and 11 represents December

getUTCMonth ()-Returns the month of the UTC date, where 0 represents January, and 11 represents December

Setmonth (month)-set the month of the date, the incoming month value must be greater than 0, and more than 11 increase the year

setUTCMonth (month)-the month in which the UTC date is set, the incoming month value must be greater than 0, and more than 11 increases the year

GetDate ()-Returns the number of days in the month of the day (1 to 31)

getUTCDate ()-Returns the number of days in the UTC date month (1 to 31)

SetDate (Day)-sets the number of days in the date month, if the value passed in exceeds the number of days that should be in the month, increase the month

SetUTCDate (Day)-sets the number of days in the UTC date month, if the value passed in exceeds the number of days that should be in the month, increase the month

GetDay ()-Returns the day of the week, where 0 represents Monday, 6 means Sunday

GetUTCDay ()-Returns the day of the week in UTC, where 0 represents Monday, and 6 represents Sunday

GetHours ()-Returns the number of hours in the date (0 to 23)

getUTCHours ()-Returns the number of hours in UTC date (0 to 23)

Sethours-Sets the number of hours in a date and the number of days in the month when the passed value exceeds 23

setUTCHours-Sets the number of hours in the UTC date and the number of days in the month when the passed value exceeds 23

Getminutes ()-Returns the number of minutes in the date (0 to 59)

getUTCMinutes ()-Returns the number of minutes in UTC date (0 to 59)

Setminutes (min)-set the number of minutes in a date, and increase the number of hours when the value passed in exceeds 59

setUTCMinutes (min)-set the number of minutes in a date, and increase the number of hours when the value passed in exceeds 59

Getseconds ()-Returns the number of seconds in the date (0 to 59)

getUTCSeconds ()-Returns the number of seconds in UTC date (0 to 59)

Setseconds (seconds)-sets the number of seconds in a date and increases the number of minutes by passing in a value greater than 59

setUTCSeconds (seconds)-sets the number of seconds in UTC date, and increases the number of minutes by passing in a value greater than 59

Getmilliseconds ()-Returns the number of milliseconds in the date

getUTCMilliseconds ()-Returns the number of milliseconds in the UTC date

Setmilliseconds (milliseconds)-set the number of milliseconds in the date

setUTCMilliseconds (milliseconds)-sets the number of milliseconds in UTC date

getTimezoneOffset ()-The number of minutes between the local time and UTC time

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.