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

在Rails中使用日期时间

  •  12
  • oldhomemovie  · 技术社区  · 15 年前

    如何正确提取 时间 只有从 日期时间 对象(对象) post.created_at (准确地说)有/没有 TimeZone ?如何提取 day , month year 价值观?如何提取 day/month/year 通过自定义模式(是否可能)?

    2 回复  |  直到 15 年前
        1
  •  30
  •   Swanand    11 年前

    字符串格式的时间:

    post.created_at.strftime("FORMAT STRING HERE")
    # Without the influence of time-zone: I take it that you want UTC
    post.created_at.utc.strftime("FORMAT STRING HERE")
    

    链接为 strftime 文档: http://ruby-doc.org/core/classes/Time.html#method-i-strftime

    获取小时、分钟和秒值:

    post.created_at.hour
    post.created_at.min
    post.created_at.sec
    
        2
  •  0
  •   upisdown    11 年前
    <%= l(@issue.created_at, format: :your_format) %>
    

    在locale yaml(app/config/locale/country.yml)中,必须声明

    time:
     formats:
      your_format: '%d %b. %Y'
      your_another_format: '%I:%M'
    

    为了便于配置,日期格式应该在i18n yaml定义文件中声明,其他日期格式可以在这里找到。