为什么这个Javascript在Firefox中不起作用? [英] Why does this Javascript not work in Firefox?

查看:121
本文介绍了为什么这个Javascript在Firefox中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一些简单的Javascript操纵日期。代码如下:

  var newDate = new Date(2013-07-23+12:00:00 ); 
console.log(newDate.toString());
newDate = new Date(newDate.getTime() - (24 * 60 * 60 * 1000));
console.log(newDate.toString());
var date = newDate.getFullYear()+ - +(0+(newDate.getMonth()+ 1))。slice(-2)+ - +(0+ newDate。 getDate())。slice(-2);
console.log(date);

本质上,我正在转换

  2013-07-23  - > 2013年7月22日12:00:00 GMT + 1000  - > 2013-07-22 

Chrome可以正常工作,您可以通过小提琴。它总是返回

 无效日期
无效日期
NaN-aN-aN

对于Firefox中的三个 console.logs ,但是:

  Tue Jul 23 2013 12:00:00 GMT + 1000(E.澳大利亚标准时间)
2013年7月22日星期一12:00 12:00 GMT + 1000(E.澳大利亚标准时间)
2013-07-22

对于Chrome。

解决方案

您的日期格式应为符合IETF的RFC 2822时间戳一些跨浏览器不一致,如果不是。



在此阅读: http://dygraphs.com/date-formats.html



但基本上 - 你应该只是替换' - '与' / '使其在任何现有的浏览器上工作


I am trying to manipulate a date with some simple Javascript. The code is as follows:

var newDate = new Date("2013-07-23" + " 12:00:00");
console.log(newDate.toString());
newDate = new Date(newDate.getTime() - (24 * 60 * 60 * 1000));
console.log(newDate.toString());
var date = newDate.getFullYear() + "-" + ("0" + (newDate.getMonth() + 1)).slice(-2) + "-" + ("0" + newDate.getDate()).slice(-2);
console.log(date);

Essentially, I am converting

2013-07-23 -> Jul 22 2013 12:00:00 GMT+1000 -> 2013-07-22

It works fine in Chrome, you can test the code via this Fiddle. It always returns

"Invalid Date"
"Invalid Date"
"NaN-aN-aN"

For the three console.logs in Firefox, but:

Tue Jul 23 2013 12:00:00 GMT+1000 (E. Australia Standard Time)
Mon Jul 22 2013 12:00:00 GMT+1000 (E. Australia Standard Time) 
2013-07-22

For Chrome.

解决方案

Your date format should be "IETF-compliant RFC 2822 timestamp" and there's some cross-browser inconsistency if it's not.

Read about it here: http://dygraphs.com/date-formats.html

But basically - you should just replace '-' with '/' to make it work on any existing browser

这篇关于为什么这个Javascript在Firefox中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆