Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I create a new Date object in IRB with a given date. The following didn't work.

1.9.3p194 :053 > require 'active_support'
 => true 
1.9.3p194 :054 > Date.new
 => #<Date:0x9d80730> 
1.9.3p194 :055 > Date.parse('12/01/2012')
NoMethodError: undefined method `parse' for Date:Class
        from (irb):55

1.9.3p194 :055 > Date.new('12/01/2012')
ArgumentError: wrong number of arguments(1 for 0)
question from:https://stackoverflow.com/questions/12544552/how-can-i-create-a-new-date-instance-in-ruby

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
85 views
Welcome To Ask or Share your Answers For Others

1 Answer

According to Date documentation:

require 'date'

Date.new(2001,2,25)           #=> #<Date: 2001-02-25 
Date.jd(2451966)             #=> #<Date: 2001-02-25 
Date.ordinal(2001,56)        #=> #<Date: 2001-02-25 
Date.commercial(2001,8,7)    #=> #<Date: 2001-02-25 
Date.parse('2001-02-25')     #=> #<Date: 2001-02-25 
Date.strptime('25-02-2001', '%d-%m-%Y') #=> #<Date: 2001-02-25 
Time.new(2001,2,25).to_date   #=> #<Date: 2001-02-25 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...