admin管理员组文章数量:1287789
I want to parse date in my page to Javascript's Date
.
So I have this in my page
<span>01-07-2012 01:04 PM</span>
And I have Javascript code that parses this value to date
var tagText = $(this).html();
var givenDate = new Date(tagText);
alert(givenDate);
And here is what I get in different browsers
IE:
Sat Jan 7 13:04:00 UTC+0400 2012
Chrome:
Sat Jan 07 2012 13:04:00 GMT +0400 (Caucasus Standard Time)
Firefox:
Invalid Date
Why Firefox doesn't recognize my date? What I must change to make it work with all major browsers?
Here is jsfiddle /
I want to parse date in my page to Javascript's Date
.
So I have this in my page
<span>01-07-2012 01:04 PM</span>
And I have Javascript code that parses this value to date
var tagText = $(this).html();
var givenDate = new Date(tagText);
alert(givenDate);
And here is what I get in different browsers
IE:
Sat Jan 7 13:04:00 UTC+0400 2012
Chrome:
Sat Jan 07 2012 13:04:00 GMT +0400 (Caucasus Standard Time)
Firefox:
Invalid Date
Why Firefox doesn't recognize my date? What I must change to make it work with all major browsers?
Here is jsfiddle http://jsfiddle/mgER5/1/
Share Improve this question edited Jan 12, 2012 at 7:20 Chuck Norris asked Jan 12, 2012 at 7:08 Chuck NorrisChuck Norris 15.2k15 gold badges95 silver badges127 bronze badges 7- what version of firefox are you using? this works fine in the new version – redmoon7777 Commented Jan 12, 2012 at 7:14
- 9.0.1 ... I guess, it's the latest version. – Chuck Norris Commented Jan 12, 2012 at 7:15
- ah ok I have 8.0 I'll upgrade now and let you know – redmoon7777 Commented Jan 12, 2012 at 7:19
- I just tested it on 9.0.1 and it works. result: "Sat Jan 07 2012 13:04:00 GMT-0500 (Eastern Standard Time)" – redmoon7777 Commented Jan 12, 2012 at 7:27
- Do you test with this format? <span>01-07-2012 01:04 PM</span> – Chuck Norris Commented Jan 12, 2012 at 7:28
4 Answers
Reset to default 9try this:
var tagText = $(this).html();
tagText = tagText.replace(/-/g, '/');
var givenDate = new Date(tagText);
alert(givenDate);
As explained in the documentation the string you are passing to the constructor of the Date object should be:
String value representing a date. The string should be in a format recognized by the parse method (IETF-pliant RFC 1123 timestamps).
Basically it should represent an RFC822 or ISO 8601 date.
What I must change to make it work with all major browsers?
Write it in milliseconds.
If you really want full cross-browser support for any date format, you should take a look at moment.js. It allows you to be explicit about the input format. For example:
var m = moment('01-07-2012 01:04 PM', 'DD-MM-YYYY hh:mm a');
本文标签: javascriptDate issue in FirefoxStack Overflow
版权声明:本文标题:javascript - Date issue in Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295103a2370771.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论