Introduction to parameter usage in Date functions in JS _ basic knowledge

Source: Internet
Author: User
This article gives you a detailed description of the parameters in the Date function in JS in actual use. If you are interested, do not miss creating a Date object. You can use the following methods:

The Code is as follows:


Var now = new Date ()


Of course, the function does not pass any parameters, indicating that the object now automatically obtains the current time.

If you want to create a custom time object, you must pass the Date () parameter. This parameter must be in milliseconds (the number of milliseconds from midnight on January 1, January 1, 1970 to the custom time ).

We can use Date. parse () and Date. UTC () to get the number of milliseconds for the custom time.

Date. parse () receives a string parameter that represents the Date, for example, "May 25, 2013", "6/13/2013", and other formats. The supported formats vary by region.

And Date. the parameters received in UTC () are the year, the number of months starting from 0 (0-11), and the day of the month (1-31) the hour tree (0-23), minute, second, and millisecond. The year and the number of months are required. The default value of other parameters is 0.

If you want to define a Date object for January 1, December 12, 2013, you can use Date. parse ():

Var mydate = new Date (Date. parse ("12/12/2013 ")),

If so:

Var mydate = new Date ("12/12/2013"), when constructing a Date, Date. parse () is automatically called to convert the Date string to the number of milliseconds.

You can also use Date. UTC ():

Var mydate = new Date (Date. UTC (, 12) // note that the subscripts of the Month start from 0, and The subscripts of the day start from 1.

If so:

Var mydate = new Date (, 12), similar to the preceding parse constructor, Date is automatically called when constructing a Date object. UTC (). If the first parameter is a numeric value, it is treated as the year, and the second parameter is month ...... note that the former var mydate = new Date (Date. UTC (, 12) obtains the GMT time, while the latter var mydate = new Date (, 12) obtains the local time based on the system settings.
Related Article

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.