javascript 在 Firefox 中将日期字符串转换为日期对象时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21984406/
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
Error while converting date string to Date object in Firefox
提问by Rahul Desai
I am converting a simple dateString to Date object. The following code works perfectly on all browsers except Firefox.
我正在将一个简单的 dateString 转换为 Date 对象。以下代码在除 Firefox 之外的所有浏览器上都能完美运行。
var dateString = "02-24-2014 09:22:21 AM";
var dateObject = new Date(dateString);
console.log(dateObject.toDateString());
The Firebug console in Firefox says Invalid Date
. What am I doing wrong here?
Firefox 中的 Firebug 控制台显示Invalid Date
. 我在这里做错了什么?
I also tried replacing -
with \
, but it didnt help.
我也试过更换-
用\
,但它没有帮助。
Is it possible to do this without using any libraries?
是否可以在不使用任何库的情况下执行此操作?
回答by Rahul Desai
Looks like Firefox does not like the -
in dateString
.
看起来 Firefox 不喜欢-
in dateString
.
Replace all occurrences of -
with /
using a regular expression and then convert the string to Date
object.
使用正则表达式替换所有出现的-
with /
,然后将字符串转换为Date
对象。
var str = '02-24-2014 09:22:21 AM';
str = str.replace(/-/g,'/'); // replaces all occurances of "-" with "/"
var dateObject = new Date(str);
alert(dateObject.toDateString());
回答by Felix Merlin Bennet
Please try this:
请试试这个:
var dateString = "02-24-2014 09:22:21 AM";
var dateString = "02-24-2014 09:22:21 AM";
var dateObject = new Date();
var dateObject = new Date();
dateObject.toDateString(dateString);
dateObject.toDateString(dateString);
回答by kamilkp
Try: var dateString = "02/24/2014 09:22:21 AM"
尝试: var dateString = "02/24/2014 09:22:21 AM"
dd-mm-yyyy is not a standard date format in EcmaScript. Some browsers implement it, some don't.
dd-mm-yyyy 不是 EcmaScript 中的标准日期格式。有些浏览器实现了它,有些没有。
You tried replaceing hyphens with baskslashes, but you need to replace them with slashes.
您尝试用 baskslashes 替换连字符,但您需要用斜杠替换它们。
if a date with hyphens comes from your server or something you can replace them using replace
method and regex:
如果带连字符的日期来自您的服务器或其他东西,您可以使用replace
方法和正则表达式替换它们:
var dateString = "02-24-2014 09:22:21 AM";
dateString = dateString.replace(/-/g, '/');
回答by govinda
i will suggest you to use,
我会建议你使用,
http://momentjs.com/
moment.js jQuery api.It works on all browers. There are many ways to do same task. but easiest way is to add moment.js.
moment.js jQuery api.It 适用于所有浏览器。有很多方法可以完成相同的任务。但最简单的方法是添加moment.js。
var dateString=moment('date as string').toDate();
回答by Alberto
I advise you to use the ISO8601 (http://en.wikipedia.org/wiki/ISO_8601) format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.
我建议您使用 ISO8601 ( http://en.wikipedia.org/wiki/ISO_8601) 格式:YYYY-MM-DD 或 YYYY-MM-DDTHH:MM:SS。
So: new Date('2014-02-24T09:22:21')
所以: new Date('2014-02-24T09:22:21')
If the string is fixed... then split it and use Date constructor like
如果字符串是固定的...然后将其拆分并使用 Date 构造函数,例如
new Date('2014', '02' - 1, '24', '09', '22', '21')