正确的Javascript Date.parse(...)格式的字符串? [英] The correct Javascript Date.parse(...) format string?

查看:90
本文介绍了正确的Javascript Date.parse(...)格式的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试过了一个文本不变的方法来构造一个字符串,使Javascript Date()构造函数可以解析它并创建适当的日期对象这些不工作的格式字符串(使用C#生成字符串):

  clientDate.ToString(); 
//给出:11/05/2009 17:35:23 +00:00

clientDate.ToString(MMM'dd','yyyy''h' 'mm':'ss''tt);
//在英文服务器上工作
//但在法语服务器上,给出:mai 11,2009 5:35:23
// Javascript不会解析。

clientDate.ToString(MM - 'dd' - 'yyyy''HH':'mm':'ss)
//给出:05-11-2009 17: 35:23

什么是通用格式?

解决方案

根据MDC


给定一个表示时间的字符串, parse 返回时间价值。它接受IETF标准( RFC 1123 第5.2.14节和其他地方)日期语法:1995年12月25日星期一13:30:00 GMT。它了解美国大陆时区缩写,但对于一般用途,请使用时区偏移,例如星期一,1995年12月25日13:30:00 GMT + 0430(格林威治子午线以东30分钟)。如果不指定时区,则假定为本地时区。 GMT和UTC被认为是等效的。


如果您无法使用英文语言环境生成此格式,请尝试使用 Date.UTC


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??

解决方案

According 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

这篇关于正确的Javascript Date.parse(...)格式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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