在 IRB 中,如果我运行以下命令:
require 'date'
Date.today
我得到以下输出:
=> #<Date: 2015-09-26 ((2457292j,0s,0n),+0s,2299161j)>
但是在 Rails 控制台中,如果我运行 Date.today
,我会得到这个:
=> Sat, 26 Sep 2015
我查看了 Rails 的 Date class但找不到 Rails 的 Date.today
显示输出与 Ruby 的输出有何不同。
谁能告诉我,在 Rails 中,Date.today
或 Date.tomorrow
如何格式化日期以便更好地显示?
最佳答案
您问题的答案是ActiveSupport
's core extension to Date
类(class)。它覆盖了 inspect
和 to_s
的默认实现:
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
def readable_inspect
strftime('%a, %d %b %Y')
end
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect
命令行示例:
ruby-2.2.0 › irb
>> require 'date'
=> true
>> Date.today
=> #<Date: 2015-09-27 ((2457293j,0s,0n),+0s,2299161j)>
>> require 'active_support/core_ext/date'
=> true
>> Date.today
=> Sun, 27 Sep 2015
关于ruby-on-rails - Ruby 和 Rails "Date.today"格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800720/