JavaScript Date function use need attention place

Source: Internet
Author: User

When we need to get the number of milliseconds in the future, the first possible way to think about it is

(new Date ("2014-12-08 12:00:00")). GetTime ()

This will return a number in the method under Chrome.

But in IE the return is a Nan, is because IE thinks YYYY-MM-DD HH:ii:ss is not a reasonable date format

But it can accept the YYYY-MM-DD format.

For universal compatibility, we can decompose the values of each unit for YYYY-MM-DD HH:ii:ss, using the Setxxxx method to get the time you want

var New Date () d.setfullyeard.setdate (8) D.setmonth (one)d.sethours (All) D.setminutes (xx) d.setseconds (00)

D.gettime ()//Returns the number of milliseconds for the 2014-12-08 12:00:00

It must be noted here that setdate must be called before setmonth.

Because in fact setmonth allow two parameters, the first parameter is the month, the second is the date

The second date parameter is not passed, and is set to the date of the day by default

If the new Date () is 31st, and your target month is not the day, D automatically sets its month to the next month with 31st

For example, you call on August 31, the target is one day in September,

// =============== called the wrong order on August 31 d.setmonth (8) d.setdate (1)//  The month of D is automatically set to 09 is the actual date of October // because the actual September does not have 31st // set the month and then set the date / / so you get D is actually the October 1 // =============== called the correct order on August 31 d.setdate (1) D.setmonth (8)

Set the setdate first, then set the Setmonth. This ensures that no errors are possible.

JavaScript Date function use need attention place

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.