W3C 格式的时间
如何以 W3C 格式呈现时间,包括小时、分钟、秒和时区偏移量。
示例:
1997-07-16T19:20:30+01:00
更新 1
我在 config/locales/my_locale.yml 中有以下声明:
time:
formats:
w3c: %Y-%m-%dT%H:%M
唯一缺少的部分是偏移量,例如“+01:00”。它的插值符号是什么?
我找到了此参考,其中包含时间区域名称,用 %Z 进行插值。
但是,offset在哪里?
How do i render time in W3C Format, including hours, minutes, seconds and timezone offset.
Example:
1997-07-16T19:20:30+01:00
Update 1
I have following declaration in config/locales/my_locale.yml:
time:
formats:
w3c: %Y-%m-%dT%H:%M
The only missing part is offset, like "+01:00". What is a interpolation symbol for it ?
I've found this reference, which contains time zone name, interpolated with %Z.
But, where is offset ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上你应该使用 %:z 来包含 + 符号。您还需要包括秒。
更新:请注意,这仅适用于 Ruby 1.9.3+,%:z 格式标记在早期版本中不可用!
Actually you should use %:z to include the + symbol. You also need to include the seconds.
Update: Note that this is only possible with Ruby 1.9.3+, the %:z format token is not available in earlier versions!
您已经得到了答案,但我只是想说,由于这不取决于用户区域设置,而是由组织定义的标准,因此最好将其保留在区域设置文件之外,例如
我个人添加
的 en.yml
application.rb
的底部You've already got the answer but I just wanted to say that since this is not depended on the user locale but a standard defined by an organization it might be better to keep it outside of locale file such as en.yml
I personally add
at the bottom of
application.rb
要插入时区偏移值,应使用%z键(小写字母)。
To insert time zone offset value, %z key (lowercase letter) should be used.
您可以添加文件
config/initializers/time_formats.rb
并放置:然后使用
somting.updated_at.to_s(:w3c)
You can add a file
config/initializers/time_formats.rb
and put:Then use
somting.updated_at.to_s(:w3c)