代码之家  ›  专栏  ›  技术社区  ›  yellowreign

Rails:将created\u at转换为PST

  •  0
  • yellowreign  · 技术社区  · 2 年前

    我想转换 created_at 按此格式,太平洋标准时间1月12日下午2:00。

    我尝试的第一步是转换 已在创建\u 现场至太平洋标准时间(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

    1 回复  |  直到 2 年前
        1
  •  0
  •   oonsk    2 年前

    由于它已经是一个日期时间,您应该可以这样做来转换它:

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

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