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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12544552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:47:29  来源:igfitidea点击:

How can I create a new Date instance in Ruby

ruby-on-railsrubydatedatetimeactivesupport

提问by Michael Durrant

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)

回答by Sébastien Le Callonnec

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 

回答by Houen

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 > 

回答by jethro

I find the following easier to remember than Date.new:

我发现以下内容比以下内容更容易记住Date.new

require 'active_support/core_ext/string'
'2019-03-31'.to_date
=> #<Date: 2019-03-21 ((2458564j,0s,0n),+0s,2299161j)>

See https://apidock.com/rails/String/to_date

https://apidock.com/rails/String/to_date

回答by rossta

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

如果您想在 Rails 之外获得对 Date 的 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

回答by Douglas G. Allen

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

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

回答by coderGuy

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

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

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

回答by saihgala

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