Home > Web Front-end > JS Tutorial > Introduction to the use of parameters in the Date function in JS_Basic knowledge

Introduction to the use of parameters in the Date function in JS_Basic knowledge

WBOY
Release: 2016-05-16 17:05:51
Original
1111 people have browsed it

To create a date object, you can use the following method:

Copy code The code is as follows:

var now=new Date()

Of course, no parameters are passed in the function, which means that this object now automatically obtains the current time.

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

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

Date.parse() receives a string parameter representing the date, such as "May 25,2013", "6/13/2013" and other formats. The specific supported formats vary by region.

The parameters received in Date.UTC() are the year, the number of months starting from 0 (0-11), the day of the month (1-31), and the hour tree (0- 23), minutes, seconds, milliseconds, the year and month are required, and other parameters default to 0.

If we want to define a date object on December 12, 2013, we can use Date.parse():

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

If:

var mydate=new Date("12/12/2013"), when constructing Date, Date.parse( will be automatically called )Convert date string to milliseconds.

You can also use Date.UTC():

var mydate=new Date(Date.UTC(2013,11,12)//Note that the month subscript starts from 0, and the day’s The subscript starts from 1

If this is the case:

var mydate=new Date(2013,11,12), similar to the parse construction method above, it will be automatically called when constructing the date object Date.UTC(), if the first parameter is a numerical value, treat it as the year, and the second parameter is the month... But note that the former var mydate=new Date(Date.UTC(2013 ,11,12) gets the GMT time, while the latter var mydate=new Date(2013,11,12) gets the local time based on the system settings.
Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template