Safari中的Javascript“无效日期”错误

时间:2009-12-05 15:38:02

标签: javascript regex date safari

我一直在尝试调试我的脚本,我无法理解这个问题:

var date = new Date("19871104071535".replace(
    /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
    '$4:$5:$6 $2/$3/$1'
));
alert(date);

适用于Firefox,IE,Chrome,但Safari会给我一个“无效日期”错误。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

TimeDate的顺序错误(我猜测只是Safari):

我在Safari中对此进行了测试并且它可以正常工作(我只是在最终字符串中交换日期和时间位置):

var date = new Date("19871104071535".replace(
    /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
    '$2/$3/$1 $4:$5:$6'
));
alert(date);

它也适用于其他浏览器,因为这是预期的。