admin管理员组文章数量:1207755
I am trying to create a date object from a string. I get date in ISO format except milliseconds part like "2012-01-30T16:23:12"
Results differ when I run following code in IE, Chrome and Firefox (Link to Fiddle)
currentDate = "2012-01-30T16:23:12";
var date = new Date(currentDate);
alert(date);
IE and Chrome considers the string as UTC but firefox considers in local time zone.
Is there any generic way to get around it except for checking user agent everywhere?
I am trying to create a date object from a string. I get date in ISO format except milliseconds part like "2012-01-30T16:23:12"
Results differ when I run following code in IE, Chrome and Firefox (Link to Fiddle)
currentDate = "2012-01-30T16:23:12";
var date = new Date(currentDate);
alert(date);
IE and Chrome considers the string as UTC but firefox considers in local time zone.
Is there any generic way to get around it except for checking user agent everywhere?
Share Improve this question edited Feb 27, 2013 at 10:52 Aaron Digulla 329k110 gold badges623 silver badges836 bronze badges asked Jan 30, 2012 at 11:06 harshaharsha 9631 gold badge13 silver badges28 bronze badges 1- possible duplicate of Annoying javascript timezone adjustment issue – Tomasz Nurkiewicz Commented Jan 30, 2012 at 11:18
3 Answers
Reset to default 16You could try appending the zero timezone offset +00:00
for UTC:
currentDate = "2012-01-30T16:23:12+00:00";
Does that help? (Sorry I can't test it without actually changing my timezone.)
Hm, possible workaround is to parse string and use methods.
setUTCDate()
setUTCFullYear()
setUTCHours()
Probably, there is better solution
There is no guarantee that the input will be correctly parsed if in the present format. The Date.parse() routine is only required to parse strings in a particular format—parsing other formats is implementation-dependent. If you dare to rely on implementations satisfying the requirement, add data to conform to the specific format:
new Date(currentDate + '.000Z')
Alternatively, use a library that can parse data in the current format, e.g. jQuery or Globalize.js.
Similar considerations apply to writing dates. There is no guarantee of the output format if you use Date.toString()
, either explicitly or as in alert(date)
. Even within a single computer, different browsers will use different localized formats.
本文标签: javascriptFirefox new Date() from string constructs time in local time zoneStack Overflow
版权声明:本文标题:javascript - Firefox new Date() from string constructs time in local time zone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738711472a2108221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论