如何在Ruby中创建一个新的Date实例

时间:2022-11-25 08:04:53

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

如何在给定日期的IRB中创建新的Date对象。以下不起作用。

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)

6 个解决方案

#1


28  

According to Date documentation:

根据日期文件:

require 'date'

Date.new(2001,2,3)           #=> #<Date: 2001-02-03 ...>
Date.jd(2451944)             #=> #<Date: 2001-02-03 ...>
Date.ordinal(2001,34)        #=> #<Date: 2001-02-03 ...>
Date.commercial(2001,5,6)    #=> #<Date: 2001-02-03 ...>
Date.parse('2001-02-03')     #=> #<Date: 2001-02-03 ...>
Date.strptime('03-02-2001', '%d-%m-%Y')
                             #=> #<Date: 2001-02-03 ...>
Time.new(2001,2,3).to_date   #=> #<Date: 2001-02-03 ...>

#2


3  

1.9.3-p125 :012 > require 'date'
 => true 
1.9.3-p125 :013 > Date::new(2012,02,03)
 => #<Date: 2012-02-03 ((2455961j,0s,0n),+0s,2299161j)> 
1.9.3-p125 :014 > 

#3


1  

If you're trying to get active_support extensions to Date outside of Rails, you'll have to use the core_ext module:

如果您尝试在Rails之外的日期获得active_support扩展,则必须使用core_ext模块:

require 'active_support/core_ext/date/calculations'
Date.parse('12/01/2012')
=> #<Date: 2012-01-12 ((2455939j,0s,0n),+0s,2299161j)>

More info in this Rails guide: http://edgeguides.rubyonrails.org/active_support_core_extensions.html

此Rails指南中的更多信息:http://edgeguides.rubyonrails.org/active_support_core_extensions.html

#4


0  

Date.strptime("2012-09-21 19:45:48","%Y-%m-%d %H:%M:%S")

#5


0  

You need to use the date extension from the Ruby Standard Library. Most examples in the docs don't always show you to require it.

您需要使用Ruby标准库中的日期扩展。文档中的大多数示例并不总是向您显示要求它。

require 'date'

was what you needed. It's not a built in set of Classes or Modules.

是你需要的。它不是一组内置的类或模块。

You get two Classes with that addition,

你得到两个类,添加,

Date
DateTime

And with that you may now use info found in the docs

有了它,您现在可以使用文档中的信息

#6


0  

require 'date'
Date::strptime("29-05-2017", "%d-%m-%Y")

So, you need to include this date module in your code and then use the method strptime inside which you may notice for year, I have used capital Y because full year is entered as the first parameter of the strptime method. You can use small y if the date is like '29-05-17'.

因此,您需要在代码中包含此日期模块,然后使用您可能注意到的年份中的方法strptime,我使用了资本Y,因为输入全年作为strptime方法的第一个参数。如果日期类似于'29 -05-17',您可以使用小y。

#1


28  

According to Date documentation:

根据日期文件:

require 'date'

Date.new(2001,2,3)           #=> #<Date: 2001-02-03 ...>
Date.jd(2451944)             #=> #<Date: 2001-02-03 ...>
Date.ordinal(2001,34)        #=> #<Date: 2001-02-03 ...>
Date.commercial(2001,5,6)    #=> #<Date: 2001-02-03 ...>
Date.parse('2001-02-03')     #=> #<Date: 2001-02-03 ...>
Date.strptime('03-02-2001', '%d-%m-%Y')
                             #=> #<Date: 2001-02-03 ...>
Time.new(2001,2,3).to_date   #=> #<Date: 2001-02-03 ...>

#2


3  

1.9.3-p125 :012 > require 'date'
 => true 
1.9.3-p125 :013 > Date::new(2012,02,03)
 => #<Date: 2012-02-03 ((2455961j,0s,0n),+0s,2299161j)> 
1.9.3-p125 :014 > 

#3


1  

If you're trying to get active_support extensions to Date outside of Rails, you'll have to use the core_ext module:

如果您尝试在Rails之外的日期获得active_support扩展,则必须使用core_ext模块:

require 'active_support/core_ext/date/calculations'
Date.parse('12/01/2012')
=> #<Date: 2012-01-12 ((2455939j,0s,0n),+0s,2299161j)>

More info in this Rails guide: http://edgeguides.rubyonrails.org/active_support_core_extensions.html

此Rails指南中的更多信息:http://edgeguides.rubyonrails.org/active_support_core_extensions.html

#4


0  

Date.strptime("2012-09-21 19:45:48","%Y-%m-%d %H:%M:%S")

#5


0  

You need to use the date extension from the Ruby Standard Library. Most examples in the docs don't always show you to require it.

您需要使用Ruby标准库中的日期扩展。文档中的大多数示例并不总是向您显示要求它。

require 'date'

was what you needed. It's not a built in set of Classes or Modules.

是你需要的。它不是一组内置的类或模块。

You get two Classes with that addition,

你得到两个类,添加,

Date
DateTime

And with that you may now use info found in the docs

有了它,您现在可以使用文档中的信息

#6


0  

require 'date'
Date::strptime("29-05-2017", "%d-%m-%Y")

So, you need to include this date module in your code and then use the method strptime inside which you may notice for year, I have used capital Y because full year is entered as the first parameter of the strptime method. You can use small y if the date is like '29-05-17'.

因此,您需要在代码中包含此日期模块,然后使用您可能注意到的年份中的方法strptime,我使用了资本Y,因为输入全年作为strptime方法的第一个参数。如果日期类似于'29 -05-17',您可以使用小y。