【问题标题】:Changing .localtime format to 12 hour format将 .localtime 格式更改为 12 小时格式
【发布时间】:2015-12-10 18:02:25
【问题描述】:

我正在创建一个显示用户何时完成证书的列。我希望完成的日期以 12 小时格式显示在用户的时区中。截至目前,我有正确的时区显示 - 但它显示在军事时间。如何以 12 小时格式显示用户时区中的时区?

这是我的 .rb 文件中的内容:

def date_earned
  if user_certificate
   user_certificate.created_at.localtime
  else
    'Not Completed'
  end
end

这是我的 .html.haml 代码行:

%span.date-earned
  =certificate_presenter.date_earned

目前在列中显示为:

2015-12-09 14:29:12 -0800

【问题讨论】:

    标签: ruby-on-rails ruby timezone haml


    【解决方案1】:

    改变这一行:

    user_certificate.created_at.localtime
    

    user_certificate.created_at.localtime.strftime("%Y-%m-%d %I:%M:%S %z")
    

    【讨论】:

      【解决方案2】:

      使用

      user_certificate.created_at.localtime.strftime("%Y-%m-%d %I:%M:%S %z")
      

      2015-12-09 02:29:12 -0800

      user_certificate.created_at.localtime.strftime("%Y-%m-%d %I:%M:%S:%P %z")
      

      “2015-12-09 02:29:12:pm -0800”

      其中 '%P' 用于标识 AM 或 PM。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-26
        • 2019-12-24
        • 1970-01-01
        • 2022-07-26
        • 2021-08-05
        • 1970-01-01
        相关资源
        最近更新 更多