在Rails中使用datetime

gmi*_*ile 13 ruby time datetime ruby-on-rails

如何正确地从datetime对象中提取时间(,准确地说)是否有/不受影响?如何提取,和值?如何通过自定义模式提取(是否可能)?post.created_atTimeZonedaymonthyearday/month/year

Swa*_*and 31

字符串格式的时间:

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")
Run Code Online (Sandbox Code Playgroud)

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

获取小时,分钟和秒值:

post.created_at.hour
post.created_at.min
post.created_at.sec
Run Code Online (Sandbox Code Playgroud)

  • 简要说明,示例代码和文档链接.一个出色的答案! (2认同)