gpt4 book ai didi

ruby - 如何从 db :datetime record in Rails 4? 格式化时间和日期

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:39 25 4
gpt4 key购买 nike

我的 View 中有一个 var 以 UTC 格式从 datetime 数据库记录返回日期和时间。输出如下所示:2014-01-21 03:13:59 UTC

我该如何格式化它? Date.parse(var) 将在 IRB 中给出 #=> Tue, 21 Jan 2014 但在 RAILS 4 中出现类型错误:“没有隐式转换 ActiveSupport::TimeWithZone 转换为字符串"

我最终只想显示日期;格式为:%m/%d/%Y 我唯一的选择不能是:to_sregex 这对吗? 有人请告诉我 RAILS 的方式。

最佳答案

使用Date#strftime :

Date.parse('2014-01-21 03:13:59 UTC').strftime('%m/%d/%Y')
# => "01/21/2014"

如果 varTimeWithZone对象,只需调用 TimeWithZone#strftime method :

var.strftime('%m/%d/%Y')

关于ruby - 如何从 db :datetime record in Rails 4? 格式化时间和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com