Rails:将 created_at 转换为 PST

Rails: Converting created_at to PST

我想在太平洋标准时间 1 月 12 日 2:00 下午将 created_at 转换为这种格式。

我尝试的第一步是将 created_at 字段转换为太平洋标准时间 (PST)。

但是,我卡住了 - 我什至无法通过这一步。

我试过这些,但都没有用:

Time.parse(self.created_at).in_time_zone('Pacific Time (US & Canada)')

time = Time.parse(self.created_at)
time.in_time_zone('Pacific Time (US & Canada)')

当我这样做时,我收到 no implicit conversion of ActiveSupport::TimeWithZone into String

我基于这些问题:

How do you convert the following time from UTC to EST in Ruby (without Rails)?

How to convert time from UTC to PST in rails

因为它已经是一个日期时间,你应该可以这样做来转换它:

self.created_at.in_time_zone('Pacific Time (US & Canada)')

不要忘记将新时区保存到数据库中的记录中。 Time.parse() 用于将字符串转换为日期时间(https://apidock.com/ruby/v2_5_5/Time/parse/class).