Analysis on the emergence of NaN in js new Date ()

Source: Internet
Author: User

This NaN is not NaN:

The program code is as follows:
Copy codeThe Code is as follows:
Var date = new Date (d );
If (! Date |! Date. getFullYear ){
Return;

}
Return S. Date. format (d, 'yyyy-mm-dd'); // The formatting function, which has nothing to do with this article.


It works normally in other browsers, but in ie, the program reports an error.
In the ie debugger, it is found that date is NaN,


If it is NaN, true will be returned for if judgment, and "" will be returned, but a strange scene has occurred,

 

The Code indicates that if false is returned, and the console results also indicate this:

 

We can see that date = NaN,! Date is false ,! NaN is true. My first response is the JS Bug in ie?
Then I did some tests.
Copy codeThe Code is as follows:
Var date = NaN;
Alert (! Date) // return true
Var date = new Date ('2014-1-1-1 ');
Alert (! Date) // return false

But I thought about it for a moment. I did the following test on the console:

 

This indicates that the getFullYear of date exists, so date is an object.

 

This also shows that date is an object rather than NaN, which is just a problem with the ie debugger.

Date. getFullYear () = "NaN
Date. getDate () = "NaN
These NaN are the real NaN
Summary:
The returned result of new Date ('date text') in ie is a special date object, which is displayed as NaN by the ie debugger, the method for returning number to its call will return NaN.

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.