Javascript new Date() 在 Chrome 中工作,但在 Firefox 中不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3257460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new Date() is working in Chrome but not Firefox
提问by pixeldev
I am creating a datetime string that looks like this: 2010-07-15 11:54:21
我正在创建一个如下所示的日期时间字符串: 2010-07-15 11:54:21
And with the following code I get invalid date in Firefox but works just fine in Chrome
使用以下代码,我在 Firefox 中得到无效日期,但在 Chrome 中运行良好
var todayDateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + seconds;
var date1 = new Date(todayDateTime);
In firefox date1 is giving me an invalid date, but in chrome its working just fine what would the main cause be?
在 firefox date1 给我一个无效的日期,但在 chrome 中它工作得很好,主要原因是什么?
采纳答案by Chris Laplante
You can't instantiate a date object any way you want. It has to be in a specific way. Here are some valid examples:
您无法以任何方式实例化日期对象。它必须以特定的方式。下面是一些有效的例子:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
or
或者
d1 = new Date("October 13, 1975 11:13:00")
d2 = new Date(79,5,24)
d3 = new Date(79,5,24,11,33,0)
Chrome must just be more flexible.
Chrome 必须更加灵活。
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
来源:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
From apsillerscomment:
来自apsillers评论:
the EMCAScript specification requires exactly one date format(i.e., YYYY-MM-DDTHH:mm:ss.sssZ) but custom date formats may be freely supported by an implementation: "If the String does not conform to that [ECMAScript-defined] format the function may fall back to any implementation-specific heuristics or implementation-specific date formats." Chrome and FF simply have different "implementation-specific date formats."
EMCAScript 规范只需要一种日期格式(即 YYYY-MM-DDTHH:mm:ss.sssZ),但实现可以自由支持自定义日期格式:“如果字符串不符合 [ECMAScript 定义的] 格式该函数可能会回退到任何特定于实现的启发式或特定于实现的日期格式。” Chrome 和 FF 只是具有不同的“特定于实现的日期格式”。
回答by Tahin Rahman
This works in all browsers -
这适用于所有浏览器 -
new Date('2001/01/31 12:00:00 AM')
新日期('2001/01/31 12:00:00 AM')
new Date('2001-01-31 12:00:00')
Format: YYYY-MM-DDTHH:mm:ss.sss
格式:YYYY-MM-DDTHH:mm:ss.sss
Details: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
详情:http: //www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
回答by Alvis
This works in most browsers as well
这也适用于大多数浏览器
new Date('2001/01/31 12:00:00')
That is the format of
格式是这样的
"yyyy/MM/dd HH:mm:ss"
回答by John Slegers
Option 1 :
选项1 :
Suppose your timestring has a format that looks like this :
假设您的时间字符串具有如下格式:
'2016-03-10 16:00:00.0'
In that case, you could do a simple regex to convert it to ISO 8601:
在这种情况下,您可以执行一个简单的正则表达式将其转换为ISO 8601:
'2016-03-10 16:00:00.0'.replace(/ /g,'T')
This would procude the following output :
这将产生以下输出:
'2016-03-10T16:00:00.0'
This is the standard datetime format, and thus supported by all browsers :
这是标准的日期时间格式,因此所有浏览器都支持:
document.body.innerHTML = new Date('2016-03-10T16:00:00.0') // THIS IS SAFE TO USE
Option 2 :
选项2:
Suppose your timestring has a format that looks like this :
假设您的时间字符串具有如下格式:
'02-24-2015 09:22:21 PM'
Here, you can do the following regex :
在这里,您可以执行以下正则表达式:
'02-24-2015 09:22:21 PM'.replace(/-/g,'/');
This, too, produces a format supported by all browsers :
这也产生了所有浏览器都支持的格式:
document.body.innerHTML = new Date('02/24/2015 09:22:21 PM') // THIS IS SAFE TO USE
Option 3 :
选项 3:
Suppose you have a time string that isn't easy to adjust to one of the well-supported standards.
假设您的时间字符串不容易调整到支持良好的标准之一。
In that case, it's best to just split your time string into different pieces and use them as individual parameters for Date:
在这种情况下,最好将您的时间字符串分成不同的部分,并将它们用作以下各项的单独参数Date:
document.body.innerHTML = new Date(2016, 2, 26, 3, 24, 0); // THIS IS SAFE TO USE
回答by Gabrielius
If you still want to create date using dashes, you can use this format:
如果您仍想使用破折号创建日期,则可以使用以下格式:
var date = new Date('2013-08-31T17:00:00Z')
But bear in mind, that it creates time according to UTC. Meaning, if you live in GMT+3 (3 hours ahead of GMT) timezone, it will add this timezone offset to the time. So the above example will have this value, if GMT+3 (note that it is hour 20:00 and not 17:00):
但请记住,它根据 UTC 创建时间。意思是,如果您居住在 GMT+3(格林威治标准时间前 3 小时)时区,它将将此时区偏移量添加到时间中。所以上面的例子会有这个值,如果 GMT+3(注意是小时 20:00 而不是 17:00):
Sat Aug 31 2013 20:00:00 GMT+0300 (FLE Standard Time)
Be sure to add 'Z' letter at the end, because otherwise Chrome and Firefox will parse the string differently (one will add time offset and the other won't).
一定要在末尾添加“Z”字母,否则 Chrome 和 Firefox 会以不同的方式解析字符串(一个会添加时间偏移,另一个不会)。
回答by SnapShot
I was having a similar issue in both Firefox and Safari when working with AngularJS. For example, if a date returned from Angular looked like this:
使用 AngularJS 时,我在 Firefox 和 Safari 中都遇到了类似的问题。例如,如果从 Angular 返回的日期如下所示:
2014-06-02 10:28:00
using this code:
使用此代码:
new Date('2014-06-02 10:28:00').toISOString();
returns an invalid date error in Firefox and Safari. However in Chrome it works fine. As another answer stated, Chrome is most likely just more flexible with parsing date strings.
在 Firefox 和 Safari 中返回无效日期错误。但是在 Chrome 中它工作正常。正如另一个答案所述,Chrome 很可能在解析日期字符串方面更加灵活。
My eventual goal was to format the date a certain way. I found an excellent library that handled both the cross browser compatibility issue and the date formatting issue. The library is called moment.js.
我的最终目标是以某种方式格式化日期。我找到了一个很好的库,可以处理跨浏览器兼容性问题和日期格式问题。该库名为moment.js。
Using this library the following code works correctly across all browsers I tested:
使用这个库,下面的代码在我测试过的所有浏览器上都能正常工作:
moment('2014-06-02 10:28:00').format('MMM d YY')
If you are willing to include this extra library into your app you can more easily build your date string while avoiding possible browser compatibility issues. As a bonus you will have a good way to easily format, add, subtract, etc dates if needed.
如果您愿意将这个额外的库包含在您的应用程序中,您可以更轻松地构建日期字符串,同时避免可能出现的浏览器兼容性问题。作为奖励,如果需要,您将拥有一种轻松格式化、添加、减去等日期的好方法。
回答by Bartek Kosa
This should work for you:
这应该适合你:
var date1 = new Date(year, month, day, hour, minute, seconds);
I had to create date form a string so I have done it like this:
我必须从字符串中创建日期,所以我这样做了:
var d = '2013-07-20 16:57:27';
var date1 = new Date(d.substr(0, 4), d.substr(5, 2), d.substr(8, 2), d.substr(11, 2), d.substr(14, 2), d.substr(17, 2));
Remember that the months in javascript are from 0 to 11, so you should reduce the month value by 1, like this:
请记住,javascript 中的月份是从 0 到 11,因此您应该将月份值减少 1,如下所示:
var d = '2013-07-20 16:57:27';
var date1 = new Date(d.substr(0, 4), d.substr(5, 2) - 1, d.substr(8, 2), d.substr(11, 2), d.substr(14, 2), d.substr(17, 2));
回答by bjo
One situation I've run into was when dealing with milliseconds. FF and IE will not parse this date string correctly when trying to create a new date.
"2014/11/24 17:38:20.177Z"They do not know how to handle .177Z. Chrome will work though.
我遇到的一种情况是在处理毫秒时。FF 和 IE 在尝试创建新日期时不会正确解析此日期字符串。
"2014/11/24 17:38:20.177Z"他们不知道如何处理.177Z。Chrome 会工作。
回答by XORG_99
Simple Solution, This works with All Browsers,
简单的解决方案,这适用于所有浏览器,
var StringDate = "24-11-2017"
var DateVar = StringDate.split("-");
var DateVal = new Date(DateVar[1] + "/" + DateVar[0] + "/" + DateVar[2]);
alert(DateVal);
回答by Akash
This is what worked for me on Firefoxand Chrome:
这对我有用,Firefox并且Chrome:
// The format is 'year-month-date hr:mins:seconds.milliseconds'
const d = new Date('2020-1-4 12:00:00.999')
// we use the `.` separator between seconds and milliseconds.
Good Luck...
祝你好运...

