"JavaScript Advanced Programming" Learning Summary Five (3)

Source: Internet
Author: User
Tags time zones

Introduction: The first two sections of the Object and array and its methods to summarize the length of a long, after summing up I feel that I still have to open a few more sections to summarize the fifth chapter, otherwise the layout of the knowledge point is indeed some confusion.

Summing up the study is really painful and happy, the pain lies in the knowledge point is really a lot, summed up very tired, how to choose the streamlined content, how typesetting are considered. And the joy is that, summing up the knowledge point can often let me calm down to look at these problems, and then warm to know the new. In short, the path of learning is no shortcut. During this period I also realized that I thought that after reading a book is the goal, often at that time a book read oneself is not learn what, now suddenly, in fact, we learn from the starting point is not reading a book or reading, but from the books to learn knowledge, that is the purpose of our reading.

Date Type:

The date type in ECMAScript is in the early java. util. Dtae type is built on, so the date type is also used for "international coordination Time", the number of milliseconds that startJanuary 1, 1970 to save the dates.

Create Date object: var now =new date ();

In the case of calling the Date constructor without a parameter, the newly created object automatically gets the current time, and if you want to get the specified time, you must pass the number of milliseconds (the number of milliseconds since January 1, 1970 ), which is cumbersome. So to simplify this process, ECMAScript created two methods:Date.parse () and DATE.UTC ().

Date.parse () receives a string parameter that represents a date, and then returns the corresponding number of milliseconds based on that parameter. ECMA-262 does not define what kind of fixed date format This function requires, so the results of this method will vary depending on the actual situation.

If the passed argument string cannot represent time, Nan is returned. But when we pass the time parameter in the date constructor, Date.parse () is also automatically called on the back table.

For example: Var now= new Date ("2018/8/8");

DATE.UTC (): The method also returns the number of milliseconds that represents the date, but it uses different information with date.parse () to build the value, DATE.UTC () is the year, based on the month of 0 (January is 0, February is 1, and so on). The month is 1--31, and the hour is 1--23. seconds in milliseconds. Only the number of years and months in these parameters is required. If the number of days is not set, the number assumes 1. If other parameters are omitted, the default is 0.

As an example: Var now=new Date (DATE.UTC (2018,0,1)); Mon Jan 2018 08:00:00 gmt+0800 You can see 0 is January.

Ps:ecmascript 5 Adds a Date.now (): Method that returns the number of milliseconds of the date and time when this method was called. So we can call this function at the beginning of a piece of code. Call this function at the end, and subtract the two to get the function running time. The functions that support this method are ie9+, Firefox, safari3+, Opera10.5, and Chrome.

Unsupported browsers can be used: var start =+new Date (); (Use the + operator to move to a string.) )

Method of inheritance for date type:

As with other types, the date type also overrides the toLocaleString (), toString (), and valueof () methods. However, the values returned by these methods are different from other types. The date type tolocalestring () returns the DateTime in a format that is appropriate for the locale of the browser.

Its toString () typically returns a date and time with time zone information. (I personally understand: actually say so much, tolocalsestring () is not what we said before, according to the environment return value?)

As for valueOf () does not return a string, but instead returns the number of milliseconds of the date, for example:

var now=new Date (DATE.UTC (2018,1,1));
Alert (now.valueof ());//1517443200000

Formatting methods for dates:

The date type also has some methods that are specifically used to format dates:

1, todatestring ()--Displays the time in a specific format (week, month, day, year).

2, toTimeString ()--Displays the time in a specific format (hours, minutes, seconds, and time zones).

3, toLocaleString ()--Displays the time in a specific time zone (week, month, day, year).

4, Tolocalsetime ()--Displays the time in a specific time zone (hours, minutes, seconds).

5, toUTCString ()--displays UTC time in a specific format.

It's really worth it. The output of these methods also varies from one browser to another. So the content displayed is different from the browser.

Date/Time component method:

So far, the remaining methods of the date type (shown in the following table) are directly obtained and set in the date value
Fixed part of the method. Note that the UTC date refers to the absence of a time zone bias (converting the date to GMT)
The date value.

Method description
GetTime ()
Returns the number of milliseconds that represents a date; same as the value returned by the ValueOf () method


SetTime (msec) sets the date in milliseconds, changing the entire date


getFullYear ()
Get a 4-digit year (such as 2007 instead of just 07)


getUTCFullYear () returns the 4-digit year of the UTC date

setFullYear (year) sets the year of the date. The incoming year value must be a 4-digit number (such as 2007 instead of just 07)


setUTCFullYear (year) sets the year of the UTC date. The incoming year value must be a 4-digit number (such as 2007 instead of just 07)


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) sets the month of the date. The incoming month value must be greater than 0, and more than 11 increases the year


setUTCMonth (month) sets the month of the UTC date. The incoming month value must be greater than 0, and more than 11 increases the year


GetDate ()
Returns the number of days in a date month (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 Sunday, 6 means Saturday)


GetUTCDay ()
Returns the day of the week in UTC (where 0 means Sunday, 6 for Saturday)


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


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


Sethours (time) sets the number of hours in the date. Increase the number of days in the month if the value passed in exceeds 23


setUTCHours (when) sets the number of hours in the UTC date. Increase the number of days in the month if the value passed in exceeds 23


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


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


Setminutes (min) Sets the number of minutes in the date. Increase the number of hours when the value passed in exceeds 59

setUTCMinutes (min) Sets the number of minutes in the UTC date. Increase the number of hours when the value passed in exceeds 59


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


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


Setseconds (seconds) sets the number of seconds in the date. An incoming value greater than 59 increases the number of minutes


setUTCSeconds (seconds) sets the number of seconds in the UTC date. An incoming value greater than 59 increases the number of minutes


Getmilliseconds ()
Returns the number of milliseconds in a date


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


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

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

getTimezoneOffset () returns the number of minutes that the local time differs from UTC time. For example, US Eastern Standard Time returns 300. This value changes when you enter daylight saving time somewhere.

-------------------------------------------------------------------------------------------------the end of this chapter-------------------- -------------------------------------------------------------------------

This chapter highlights attribute methods for date types in reference types.

The following section previews: Regular expressions, and function types.

"JavaScript Advanced Programming" Learning Summary Five (3)

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.