字符串上下文中的新日期在Firefox中不起作用

时间:2012-03-22 23:45:00

标签: javascript firefox node.js google-chrome

以下代码中的最后一个日志在Firefox中不起作用。为什么呢?

(function() {

    String.prototype.toDate = function() {
        return new Date(this);
    };

    console.log(Date.parse("2012-01-31"));
    console.log(new Date("2012-01-31"));
    console.log("2012-01-31".toDate());

})();

要在浏览器中对此进行测试,我将上面的代码段放入文件中并使用以下HTML。

<!DOCTYPE html>
<body>
    <script src="wtf.js"></script>
</body>

NodeJS(v0.4.12):

1327932000000
Mon, 30 Jan 2012 14:00:00 GMT 
Mon, 30 Jan 2012 14:00:00 GMT

Chrome(17.0.963.79):

1327968000000
Tue Jan 31 2012 10:00:00 GMT+1000 (EST)
Tue Jan 31 2012 10:00:00 GMT+1000 (EST)

Firefox(10.0):

1327968000000
Date {Tue Jan 31 2012 10:00:00 GMT+1000 (EST)}
Date {Invalid Date}

1 个答案:

答案 0 :(得分:0)

Firefox的String.prototype中的

this似乎没有引用字符串作为字符串。如果您添加到您的方法:

String.prototype.toDate = function() {
        return new Date(String(this));
    };

它可以正常工作。