Home > Web Front-end > JS Tutorial > body text

Analysis on NaN occurrence in js new Date()_javascript skills

WBOY
Release: 2016-05-16 17:49:02
Original
1469 people have browsed it

This NaN is not NaN:

The program code is as follows:

Copy code The code is as follows:

var date =new Date(d);
if(!date || !date.getFullYear) {
return;

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


It works fine in other browsers, but under IE, the program reports an error
Under the IE debugger, it is found that date is NaN, as shown in the figure:


If it is NaN, then the if judgment will return true or "", but a strange scene happened,

The code shows that the if judgment returns false, and the result of the console also illustrates this:



It can be seen that date = NaN, but! date is false,!NaN is true, my first reaction is, is it a JS bug?
Then I did some tests

Copy the code The code is 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 and it still wasn’t quite right, so I did the following test on the console:

This shows that the getFullYear of date exists, then date is an object

This also shows that date is an object instead of NaN, which is just a problem of the IE debugger

date.getFullYear() =》NaN
date.getDate() =》NaN
These NaN are real NaN
Summary:
New Date('date text' under ie ) is a special date object, but the IE debugger displays it as NaN, and any method that returns number will return NaN.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!