在 Rails 中将时间从一个时区转换为另一时区

2024-03-13

My created_at时间戳以 UTC 格式存储:

>> Annotation.last.created_at
=> Sat, 29 Aug 2009 23:30:09 UTC +00:00

如何将其中之一转换为“东部时间(美国和加拿大)”(考虑夏令时)?就像是:

Annotation.last.created_at.in_eastern_time

使用 DateTime 类的 in_time_zone 方法

Loading development environment (Rails 2.3.2)
>> now = DateTime.now.utc
=> Sun, 06 Sep 2009 22:27:45 +0000
>> now.in_time_zone('Eastern Time (US & Canada)')
=> Sun, 06 Sep 2009 18:27:45 EDT -04:00
>> quit

所以对于你的具体例子

Annotation.last.created_at.in_time_zone('Eastern Time (US & Canada)')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Rails 中将时间从一个时区转换为另一时区 的相关文章

随机推荐