new Date()在Chrome中工作,而不是Firefox。

时间:2022-12-06 18:02:23

I am creating a datetime string that looks like this: 2010-07-15 11:54:21

我正在创建一个datetime字符串,它看起来是这样的: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中它的工作很好,主要原因是什么?

12 个解决方案

#1


68  

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 apsillers comment:

从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规范要求确切的日期格式(即:但是自定义日期格式可以由实现*支持:“如果字符串不符合[ecmas- defined]格式,则函数可能会返回到任何特定于实现的启发式或特定于实现的日期格式。”Chrome和FF只是有不同的“特定于实现的日期格式”。

#2


19  

This works in all browsers -

这适用于所有的浏览器。

new Date('2001/01/31 12:00:00 AM')

新日期(“2001/01/31 12:00:00点”)

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

#3


10  

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"

#4


8  

If you still want to create date using dashes, you can use this format:

如果您仍然希望使用dashes创建日期,您可以使用以下格式:

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创造时间的。意思是,如果你住在格林尼治标准时间+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将会以不同的方式解析字符串(一个会增加时间偏移,另一个不会)。

#5


7  

Option 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 :

在这种情况下,可以使用一个简单的regex将其转换为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 :

这是标准的datetime格式,因此受到所有浏览器的支持:

document.body.innerHTML = new Date('2016-03-10T16:00:00.0') // THIS IS SAFE TO USE

Option 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 :

在这里,您可以执行以下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 :

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 :

在这种情况下,最好将时间字符串分割成不同的部分,并将它们作为单独的参数使用:

document.body.innerHTML = new Date(2016, 2, 26, 3, 24, 0); // THIS IS SAFE TO USE

#6


5  

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中都遇到了类似的问题。例如,如果一个从角度返回的日期是这样的:

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.

如果您愿意将此额外的库包含到应用程序中,您可以更轻松地构建日期字符串,同时避免可能的浏览器兼容性问题。作为奖励,你将有一个很好的方法来轻松地格式化,加减,等日期如果需要。

#7


4  

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));

#8


1  

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不会正确地解析这个日期字符串。“他们不知道如何处理。177z。”Chrome将工作。

#9


0  

In fact, Chrome is more flexible to deal with different string format. Even if you don't figure out its String format, Chrome still can successfully convert String to Date without error. Like this:

实际上,Chrome更灵活,处理不同的字符串格式。即使您不知道它的字符串格式,Chrome仍然可以成功地将字符串转换为日期而没有错误。是这样的:

  var outputDate = new Date(Date.parse(inputString));

But for Firefox and Safari, things become more complex. In fact, in Firefox's document, it already says: (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

但是对于Firefox和Safari来说,事情变得更加复杂。事实上,在Firefox的文档中,它已经说了:(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).

表示RFC2822或ISO 8601日期的字符串(可以使用其他格式,但结果可能是意外的)。

So, when you want to use Date.parse in Firefox and Safari, you should be careful. For me, I use a trick method to deal with it. (Note: it might be not always correct for all cases)

所以,当你想使用日期的时候。在Firefox和Safari中解析,应该小心。对我来说,我用了一个技巧来对付它。(注意:对于所有的情况,它可能并不总是正确的)

if (input.indexOf("UTC") != -1) {
  var tempInput = inputString.substr(0, 10) + "T" + inputString.substr(11, 8) + "Z";
  date = new Date(Date.parse(tempInput));
}

Here it converts 2013-08-08 11:52:18 UTC to 2013-08-08T11:52:18Z first, and then its format is fit words in Firefox's document. At this time, Date.parse will be always right in any browser.

在这里,它首先转换到2013-08-08 11:52:18 UTC到2013-08-08T11:52:18Z,然后它的格式在Firefox的文档中是合适的单词。在这个时候,日期。在任何浏览器中解析都是正确的。

#10


0  

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);

#11


-1  

In Firefox, any invalid Date is returned as a Date object as Date 1899-11-29T19:00:00.000Z, therefore check if browser is Firefox then get Date object of string "1899-11-29T19:00:00.000Z".getDate(). Finally compare it with the date.

在Firefox中,任何无效的日期作为日期对象作为日期对象返回,日期为1899-11- 29t19:00 . 0000 z,因此检查浏览器是否是Firefox,然后获取string“1899-11- 29t19:00 . 000z”. getdate()的日期对象。最后将它与日期进行比较。

#12


-1  

I have used following date format and it's working in all browser.

我已经使用了以下日期格式,它在所有浏览器中工作。

var target_date = new Date("Jul 17, 2015 16:55:22").getTime();

var days, hours, minutes, seconds;

var countdown = document.getElementById("countdown");

remaining = setInterval(function () {

    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;
    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;
    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);
    countdown.innerHTML = "<b>"+days + " day, " + hours + " hour, "
        + minutes + " minute, " + seconds + " second.</b>";  
}, 1000);

#1


68  

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 apsillers comment:

从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规范要求确切的日期格式(即:但是自定义日期格式可以由实现*支持:“如果字符串不符合[ecmas- defined]格式,则函数可能会返回到任何特定于实现的启发式或特定于实现的日期格式。”Chrome和FF只是有不同的“特定于实现的日期格式”。

#2


19  

This works in all browsers -

这适用于所有的浏览器。

new Date('2001/01/31 12:00:00 AM')

新日期(“2001/01/31 12:00:00点”)

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

#3


10  

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"

#4


8  

If you still want to create date using dashes, you can use this format:

如果您仍然希望使用dashes创建日期,您可以使用以下格式:

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创造时间的。意思是,如果你住在格林尼治标准时间+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将会以不同的方式解析字符串(一个会增加时间偏移,另一个不会)。

#5


7  

Option 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 :

在这种情况下,可以使用一个简单的regex将其转换为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 :

这是标准的datetime格式,因此受到所有浏览器的支持:

document.body.innerHTML = new Date('2016-03-10T16:00:00.0') // THIS IS SAFE TO USE

Option 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 :

在这里,您可以执行以下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 :

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 :

在这种情况下,最好将时间字符串分割成不同的部分,并将它们作为单独的参数使用:

document.body.innerHTML = new Date(2016, 2, 26, 3, 24, 0); // THIS IS SAFE TO USE

#6


5  

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中都遇到了类似的问题。例如,如果一个从角度返回的日期是这样的:

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.

如果您愿意将此额外的库包含到应用程序中,您可以更轻松地构建日期字符串,同时避免可能的浏览器兼容性问题。作为奖励,你将有一个很好的方法来轻松地格式化,加减,等日期如果需要。

#7


4  

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));

#8


1  

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不会正确地解析这个日期字符串。“他们不知道如何处理。177z。”Chrome将工作。

#9


0  

In fact, Chrome is more flexible to deal with different string format. Even if you don't figure out its String format, Chrome still can successfully convert String to Date without error. Like this:

实际上,Chrome更灵活,处理不同的字符串格式。即使您不知道它的字符串格式,Chrome仍然可以成功地将字符串转换为日期而没有错误。是这样的:

  var outputDate = new Date(Date.parse(inputString));

But for Firefox and Safari, things become more complex. In fact, in Firefox's document, it already says: (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

但是对于Firefox和Safari来说,事情变得更加复杂。事实上,在Firefox的文档中,它已经说了:(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).

表示RFC2822或ISO 8601日期的字符串(可以使用其他格式,但结果可能是意外的)。

So, when you want to use Date.parse in Firefox and Safari, you should be careful. For me, I use a trick method to deal with it. (Note: it might be not always correct for all cases)

所以,当你想使用日期的时候。在Firefox和Safari中解析,应该小心。对我来说,我用了一个技巧来对付它。(注意:对于所有的情况,它可能并不总是正确的)

if (input.indexOf("UTC") != -1) {
  var tempInput = inputString.substr(0, 10) + "T" + inputString.substr(11, 8) + "Z";
  date = new Date(Date.parse(tempInput));
}

Here it converts 2013-08-08 11:52:18 UTC to 2013-08-08T11:52:18Z first, and then its format is fit words in Firefox's document. At this time, Date.parse will be always right in any browser.

在这里,它首先转换到2013-08-08 11:52:18 UTC到2013-08-08T11:52:18Z,然后它的格式在Firefox的文档中是合适的单词。在这个时候,日期。在任何浏览器中解析都是正确的。

#10


0  

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);

#11


-1  

In Firefox, any invalid Date is returned as a Date object as Date 1899-11-29T19:00:00.000Z, therefore check if browser is Firefox then get Date object of string "1899-11-29T19:00:00.000Z".getDate(). Finally compare it with the date.

在Firefox中,任何无效的日期作为日期对象作为日期对象返回,日期为1899-11- 29t19:00 . 0000 z,因此检查浏览器是否是Firefox,然后获取string“1899-11- 29t19:00 . 000z”. getdate()的日期对象。最后将它与日期进行比较。

#12


-1  

I have used following date format and it's working in all browser.

我已经使用了以下日期格式,它在所有浏览器中工作。

var target_date = new Date("Jul 17, 2015 16:55:22").getTime();

var days, hours, minutes, seconds;

var countdown = document.getElementById("countdown");

remaining = setInterval(function () {

    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;
    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;
    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);
    countdown.innerHTML = "<b>"+days + " day, " + hours + " hour, "
        + minutes + " minute, " + seconds + " second.</b>";  
}, 1000);