JavaScript Date Object

Source: Internet
Author: User

I. Summary

The date type in ECMAScript is built on the basis of the Java.until.Date class of Java earlier. The date type is used from UTC (Coordinated uiniversal time, international coordination Hour) Midnight January 1, 1970 ( 0 o'clock) Start by the number of milliseconds to save the date. This data storage format enables date types to be saved for 100 million years before or after January 1, 1970.

Two. Create a Date Object

We know that the creation of the object is created by using the new Operation multibyte constructor object (), so you know that the Date object is created by the new operator in conjunction with the constructor date (). The structure is as follows:

var date = new Date ();

Without passing in parameters, the date () object received by the variable automatically gets the current date and time. If you want to create a specific date object, you need to pass in the parameter, which is the number of milliseconds for that particular date (that is, the number of milliseconds elapsed from midnight UTC January 1, 1970 to that particular date) ). This may sound strange. How do we know the number of milliseconds for that particular date?

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

    1. Date.parse (): This method takes a string parameter that represents a date, and then returns the corresponding number of milliseconds based on the string parameter. The format of this date may vary by region, in other words, in China will be in the format of Beijing time, in the United States will be in the United States format, as shown in:

For example, to create a Date object for December 2, 2017, the code such as:

In fact, it is possible not to write this method in the arguments, which means that the above example is equivalent to the following:

If the passed string argument cannot represent a date, then it returns Nan, and the object created by the constructor returns a invalid date (invalid date). As shown in:

Date objects have strange behavior in different browser implementations. For example: In the parsing of "2018/1/32 (according to the common sense is no 32 days this argument, will only return invalid date)", some browsers will parse it as: "2018/2/1", and Oprea is more strange, will return the current date, for example, it is March 5, 2018, which means it will return "2018/3/5". As shown in some weird behavior (Oprea browser test):

2.DATE.UTC (): This method also returns the number of milliseconds of the date. It differs from the parameters of the Date.parse () method, and its parameters are somewhat complex, namely the year, based on the month of 0 (January is 0, February is 1, and so on), which day of the month (1 to 31st), the number of hours ( 0 to 23), minutes, seconds, and milliseconds. The first and second parameters are required, if the third argument is not provided, the number of days is assumed to be 1, and if other parameters are omitted, all are assumed to be 0.

As an example:

Similarly, this method can be omitted, unlike Date.parse (), where dates and times are created based on local regions rather than GMT. The parameter must be the same as this method. As shown in:

ECMAscript5 also added the Date.now () method, which represents the number of milliseconds that the date and time of the method was called. This method simplifies the work of the Date object parsing code. As shown in:

Browsers that support this approach have ie9,firefox3+,safari3+,opera10.5+ and chrome. In browsers that do not support it, you can use the + operator to get the timestamp of a Date object. The code is as follows:

It also shows that a for loop consumes at least 3 milliseconds of time.

Three. Inheritance methods

Since the date type is also an object, the method that the object has, the date type, also has. For example, valueof () method, ToString () method, toLocaleString () method. However, the values returned by these three methods are different from the values returned by other reference types. As shown in:

As you can see, the ValueOf () method mainly returns the number of milliseconds of the date. toLocaleString () returns the current DateTime, and ToString () returns GMT China standard Time, although the exact value will vary by region and the same will vary by browser.

We can use comparison operators to compare the values returned from the valueof () method, as shown in:

As shown, I made a comparison with the current time on March 6, 2018, that is, the number of milliseconds returned using the valueof () method. In common sense, March 6, 2018 is more than the current date time (i.e. 2018/3/7 ...) is small, so the first time it is more natural to return false, the second comparison returns TRUE. This provides a great convenience for us to compare dates.

Date types In addition to the above inheritance methods, there are also methods that are formatted as strings. Mainly includes the following:

    1. toDateString (): Shows the weeks, months, days, and years. As shown in:

2.toTimeString (): Displays the current GMT time. As shown in:

3.toLocaleDateString (): Displays the day of the month. As shown:

4.toLocaleTimeString (): Shows a specific time (hour, minute, second, also indicates whether it is morning or noon or afternoon). As shown in:

5.toUTCString (): Displays the current GMT time. As shown in:

Of course, the values returned by the above methods will vary depending on the browser and region. In addition, there is a togmtstring () method equivalent to the toUTCString () method for compatibility. The two methods are equivalent. As shown in:

Of course, in addition to the date format method, there are some date-time component methods, these methods a bit more, do not have to remember all, need to use the time to read the data query. I don't have to summarize, however, I remember a few common ones, such as gettime (), which indicate the number of milliseconds to get the current time, getFullYear (), which represents the year. GetDay (), gets the day. GetMonth () Gets the month, Gethour (), gets the Hour, getminutes (), gets the minute, and getseconds () gets the seconds.

JavaScript Date Object

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.