我有一个简单的问题。我有一个带有 created_at 的实例变量。我如何将其转换为 Month, day, year ~ September, 13, 1987
当我尝试 = @example.created_at 在我看来它给了我 1987-09-13
奇怪的是,当我在控制台中执行此方法时,我得到了 Sun, 13 Sep 1987
- 如何将我的变量转换为月、日、年?
- 为什么它在控制台中返回不同的东西?
最佳答案
您可以通过指定格式来更改日期的显示方式,例如
@example.created_at.strftime("%B, %d, %Y")
# => "September, 20, 2013"
查看各种格式选项 here
由于默认格式不同,控制台和 View 中的显示格式不同。
关于ruby-on-rails - rails : to_date with a month/day/year format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18925274/