About JS New Date () The analysis _javascript technique of Nan

Source: Internet
Author: User
This nan is not nan:

The program code is as follows:
Copy Code code as follows:

var date =new date (d);
if (!date | |!date.getfullyear) {
Return

}
Return S.date.format (d, ' yyyy-mm-dd ');//Format function, not related to this article


Normal in other browsers, but under IE, the program complains
Under the IE debugger, the date is found as Nan, as shown in figure:


If it is Nan, then if the If judgment returns True, return "", but the weird scene happens,

The code shows that if judgment returns false, the console results also illustrate this point:



You can see date = NaN, but! Date is false,! NaN is true, my first reaction, ie under JS bug?
And then I did some tests.

Copy Code code as follows:

var date = NaN;
Alert (!date)//return True
var date = new Date (' 2011-1-1 ');
Alert (!date)//return False

But I thought about it or not, and I did the following test on the console:

The getfullyear of the date is present, so date is an object

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

Date.getfullyear () = "NaN
Date.getdate () = "NaN
These nan are the real Nan
Summarize:
The result returned by IE under new Date (' Date text ') is a special date object, except that the IE debugger displays it as Nan, and the method that returns number on 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.