JS Date object and acquisition system current time explanation _ javascript skills

Source: Internet
Author: User
This article mainly introduces the JS Date object and obtains the current time of the system. If you need it, you can refer to it and hope to help you. I. Date object
One object every day. Let's take a look at the Date object today. You must be familiar with the Date object. This special time effect should have been used in many places on the website. Let's recall the Date object today.

Let's first look at his definition:
DateObj = new Date ()
DateObj = new Date (dateVal)
DateObj = new Date (year, month, date [, hours [, minutes [, seconds [, MS])

Required. If it is a numeric value, dateVal indicates the number of milliseconds of the global standard time between the date and midnight, January 1, January 1, 1970. If it is a string, dateVal is parsed according to the rules in the parse method. The dateVal parameter can also be an ActiveX®The value of VT_DATE returned by the object. Required. The complete year, for example, 1976 (instead of 76 ). Required. Indicates the month, which is an integer from 0 to 11 (January 1, January to January 1, December ). Required. Represents the date, which is an integer from 1 to 31. Optional. Millisecond, an integer from 0 to 999.
The Date object is saved in milliseconds to indicate a specific time period. If the value of a parameter is greater than its range or is a negative number, other values stored will be adjusted accordingly. For example, if 150 seconds are specified, JScript redefines the number as 2 minutes 30 seconds.

If the number is NaN, the object does not represent a specific time period. If a parameter is not passed to the Date object, it is initialized to the Current Time (UTC ). Assign a value to this object before it can be used.

The Date object can represent a Date range of approximately January 1, 1970 years before and after January 1, 285,616.

The Date object has two static methods that can be called without creating a Date object. They are parse and UTC.

There are many methods for Date objects, and they are also very simple and will not be listed here. OK, See Next!

Note:

**************************************** **********************

Var myDate = new Date (2007,11, 23 );
Alert ("year =" + myDate. getYear () + "******* month =" + myDate. getMonth () + "********* day =" + myDate. getDate ());

The result after running is year = 2006 ******* month = 11 ********** day = 23.

**************************************** **********************

In this way, the display is normal. This problem occurs when the new Date value is 12 months.

**************************************** **********************

Var myDate = new Date (2007,12, 23 );
Alert ("year =" + myDate. getYear () + "******* month =" + myDate. getMonth () + "********* day =" + myDate. getDate ());

The result after running is year = 2007 ******* month = 0 ********* day = 23.

**************************************** **********************

This is a strange thing. Finally, we can find that the value of month is 0 ~ The value of 12 cannot be input. The solution is to set month-1 when the value is passed, and then add 1 when the value is retrieved. I don't know who else has a better way.

For example:

Var date = new Date (2010,0-); // here 0 represents February, minus 1 is changed to February
Alert (date. getYear () + "," + (date. getMonth () + 1) + "," + date. getDate ());

Var date = new Date (2010,12 );
Alert (date. getYear () + "," + (date. getMonth () + 1) + "," + date. getDate ());

The parameter is a string in the format of 2010/1/6.

Example:
Var txtDepartureDate = '2017-1-6'
Alert (txtDepartureDate. replace (/-/g, '/'); // use regular expressions to replace all '-'/
Var date = new Date (txtDepartureDate. replace (/-/g ,'/'));

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.