类型错误 - 即使两者都是数字,也返回NaN

时间:2014-05-30 05:20:20

标签: javascript date type-conversion nan

我尝试找出 diff currentDate.getTimezoneOffset 之间的差异。 我已经检查过它们都是数字,但它显示的是NaN。

这是 js代码:

$(document).ready(function() {

    // Grab the current date
    var currentDate = new Date();

    // Set some date in the future. In this case, it's always Jan 1
    var futureDate = new Date(currentDate.getFullYear() + 0, 8, 4);

    // Calculate the difference in seconds between the future and current date
    var diff = futureDate.getTime() / 1000 - currentDate.getTime() / 1000;


    console.log(typeof parseInt(currentDate.getTimezoneOffset()))
    console.log(typeof diff)
    console.log(parseInt(currentDate.getTimezoneOffset()))
    console.log(diff)
    console.log(diff - parseInt(currentDate.getTimezoneOffset))

});

任何人都可以向我解释为什么会发生这种情况以及如何解决它?

1 个答案:

答案 0 :(得分:3)

这里的语法错误:

   console.log(typeof parseInt(currentDate.getTimezoneOffset))

应该是

console.log(typeof parseInt(currentDate.getTimezoneOffset()))
                                                         ^^ missing brackets here