在Firefox中将日期字符串转换为Date对象时出错 [英] Error while converting date string to Date object in Firefox

查看:44
本文介绍了在Firefox中将日期字符串转换为Date对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个简单的dateString转换为Date对象.以下代码可在除Firefox之外的所有浏览器上完美运行.

I am converting a simple dateString to Date object. The following code works perfectly on all browsers except Firefox.

var dateString = "02-24-2014 09:22:21 AM";

var dateObject = new Date(dateString);

console.log(dateObject.toDateString());

Firefox中的Firebug控制台显示 Invalid Date .我在这里做什么错了?

The Firebug console in Firefox says Invalid Date. What am I doing wrong here?

我还尝试用 \ 替换-,但这没有帮助.

I also tried replacing - with \, but it didnt help.

是否可以在不使用任何库的情况下做到这一点?

Is it possible to do this without using any libraries?

推荐答案

貌似Firefox不喜欢 dateString 中的-.

Looks like Firefox does not like the - in dateString.

使用正则表达式用/替换所有出现的-,然后将字符串转换为 Date 对象.

Replace all occurrences of - with / using a regular expression and then convert the string to Date object.

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

这篇关于在Firefox中将日期字符串转换为Date对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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