Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby和Rails“;日期:今天“;格式_Ruby On Rails_Ruby_Date_Activesupport - Fatal编程技术网

Ruby on rails Ruby和Rails“;日期:今天“;格式

Ruby on rails Ruby和Rails“;日期:今天“;格式,ruby-on-rails,ruby,date,activesupport,Ruby On Rails,Ruby,Date,Activesupport,在IRB中,如果我运行以下命令: require 'date' Date.today 我得到以下输出: => #<Date: 2015-09-26 ((2457292j,0s,0n),+0s,2299161j)> 我查看了Rails,但找不到Rails的日期。今天显示的输出与Ruby的不同 在Rails中,有人能告诉我们今天的日期是如何显示的吗?今天的日期是如何显示的?明天的日期是如何显示的?Rails或方法可以满足您的需要 例如,使用来 2.2.1 :004 >

在IRB中,如果我运行以下命令:

require 'date'
Date.today
我得到以下输出:

=> #<Date: 2015-09-26 ((2457292j,0s,0n),+0s,2299161j)> 
我查看了Rails,但找不到Rails的
日期。今天
显示的输出与Ruby的不同

在Rails中,有人能告诉我们今天的日期是如何显示的吗?今天的日期是如何显示的?明天的日期是如何显示的?

Rails或方法可以满足您的需要

例如,使用

2.2.1 :004 > Date.today.to_s(:long)
 => "September 26, 2015" 
2.2.1 :005 > Date.today.to_s(:short)
 => "26 Sep" 
# 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
如果运行此命令:

require 'date'
p Date.today.strftime("%a, %e %b %Y")

你会得到这样的回答:“2015年9月26日,星期六”

你的问题的答案是课堂。它将覆盖
检查
的默认实现,以

2.2.1 :004 > Date.today.to_s(:long)
 => "September 26, 2015" 
2.2.1 :005 > Date.today.to_s(:short)
 => "26 Sep" 
# 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-2.2.0›irb
>>需要“日期”
=>正确
>>日期:今天
=> #
>>需要“主动支持/核心支持/扩展/日期”
=>正确
>>日期:今天
=>太阳报,2015年9月27日

Hi Brito,我的问题是什么时候运行Date。今天,rails如何显示=>2015年9月26日的Sat?@Lalu可能是因为rails在初始化日期时调用了
。不过,我还需要做更多的研究来证实。是的,布里托,我也是这么想的,但我想不出来。如果你发现了,请更新你的答案。无论如何谢谢你!谢谢Alexey,这正是我想要的!