admin管理员组文章数量:1285887
What is a culture-invariant way of constructing a string such that the Javascript Date() constructor can parse it and create the proper date object?
I have tried these format strings which don't work (using C# to generate the strings):
clientDate.ToString();
// gives: "11/05/2009 17:35:23 +00:00"
clientDate.ToString("MMM' 'dd', 'yyyy' 'h':'mm':'ss' 'tt");
// works on an English server
// but on a French server, gives: "mai 11, 2009 5:35:23"
// Javascript won't parse that.
clientDate.ToString("MM'-'dd'-'yyyy' 'HH':'mm':'ss")
// gives: 05-11-2009 17:35:23
What is the universal format??
What is a culture-invariant way of constructing a string such that the Javascript Date() constructor can parse it and create the proper date object?
I have tried these format strings which don't work (using C# to generate the strings):
clientDate.ToString();
// gives: "11/05/2009 17:35:23 +00:00"
clientDate.ToString("MMM' 'dd', 'yyyy' 'h':'mm':'ss' 'tt");
// works on an English server
// but on a French server, gives: "mai 11, 2009 5:35:23"
// Javascript won't parse that.
clientDate.ToString("MM'-'dd'-'yyyy' 'HH':'mm':'ss")
// gives: 05-11-2009 17:35:23
What is the universal format??
Share Improve this question asked Jul 6, 2009 at 19:24 Jeff Meatball YangJeff Meatball Yang 39k27 gold badges93 silver badges125 bronze badges 3- good question, this is a tough problem – annakata Commented Jul 6, 2009 at 19:38
- Why do you want a single string for that? – Boldewyn Commented Jul 6, 2009 at 19:56
- I need to emit a string to the browser so I can then use Javascript to localize the string, using Javascript's toLocaleString() function. However, I've had difficulty figuring out a successful, single string format that will be emitted by all my servers. – Jeff Meatball Yang Commented Jul 6, 2009 at 21:00
2 Answers
Reset to default 19According to MDC:
Given a string representing a time,
parse
returns the time value. It accepts the IETF standard (RFC 1123 Section 5.2.14 and elsewhere) date syntax:"Mon, 25 Dec 1995 13:30:00 GMT"
. It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example,"Mon, 25 Dec 1995 13:30:00 GMT+0430"
(4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.
If you can’t generate this format using english locale, try to use Date.UTC
If you want a locale-independent format Javascript can parse, you can use 2013-03-31T16:36:57+0900
. It works at least in Node.js and Chrome, so I suspect it's standard.
本文标签: parsingThe correct Javascript Dateparse() format stringStack Overflow
版权声明:本文标题:parsing - The correct Javascript Date.parse(...) format string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738410966a2085320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论