将日期解析为Rails友好格式

时间:2023-01-14 16:10:56

I've got a batch of a 100k or so records that I'm importing to a Rails app.

我有一批100k左右的记录,我正在导入到Rails应用程序。

There's a date in each row formatted like the following: 03/17/81

每行的日期格式如下:03/17/81

However, when I try to just assign it to a date field, it gets converted as follows:

但是,当我尝试将其分配给日期字段时,它将按如下方式转换:

ruby-1.8.7-p174 > "03/17/81".to_date
 => Sat, 17 Mar 0081 

Ultimately I would like the format to result in 1981-03-17

最终我希望这种格式能够在1981-03-17产生

Would this best be done on the import or should I be overriding in my Rails config to set the date standard to the format I need and what's the right approach in Rails 2.3.5?

最好是在导入时完成,还是应该覆盖我的Rails配置,将日期标准设置为我需要的格式以及Rails 2.3.5中的正确方法?

4 个解决方案

#1


19  

Use

使用

d = Date.strptime("03/17/81", "%m/%d/%y")

To get it out in 1981-03-17 use:

要在1981-03-17获得它,请使用:

d.to_s

See Date Rubydoc.

见日期Rubydoc。

#2


3  

The proper way to do this, if you are going to reuse the format in multiple places is to do something like this: http://seanbehan.com/ruby-on-rails/custom-date-formats-for-your-rails-application/

如果要在多个地方重复使用该格式,正确的方法是执行以下操作:http://seanbehan.com/ruby-on-rails/custom-date-formats-for-your-rails -应用/

Create a file config/initializers/date_formats.rb

创建文件config / initializers / date_formats.rb

...containing this:

......包含这个:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :short_date => "%Y/%m/%d"
)

Then you should see:

然后你应该看到:

ruby-1.8.7-p174 > date = "03/17/81".to_date
ruby-1.8.7-p174 > date.to_s(:short_date)
#=> "1981/03/17"

#3


1  

Try Chronic (gem install chronic):

尝试Chronic(宝石安装慢性):

require 'rubygems'
require 'chronic'
puts Chronic.parse "03/17/81"
#=> Tue Mar 17 12:00:00 -0800 1981

Returns a Time object.

返回Time对象。

#4


0  

Don't change Rails, change your code. I don't see any problem in creating a method that converts the string into a suitable date.

不要更改Rails,更改代码。我没有看到创建将字符串转换为合适日期的方法的任何问题。

string = "03/17/81"
date   = if string =~ %r{(\d+)/(\d+)/(\d+)}
  Date.parse("19#{$3}-#{$1}-#{$2}")
else
  nil # failover
end

#1


19  

Use

使用

d = Date.strptime("03/17/81", "%m/%d/%y")

To get it out in 1981-03-17 use:

要在1981-03-17获得它,请使用:

d.to_s

See Date Rubydoc.

见日期Rubydoc。

#2


3  

The proper way to do this, if you are going to reuse the format in multiple places is to do something like this: http://seanbehan.com/ruby-on-rails/custom-date-formats-for-your-rails-application/

如果要在多个地方重复使用该格式,正确的方法是执行以下操作:http://seanbehan.com/ruby-on-rails/custom-date-formats-for-your-rails -应用/

Create a file config/initializers/date_formats.rb

创建文件config / initializers / date_formats.rb

...containing this:

......包含这个:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :short_date => "%Y/%m/%d"
)

Then you should see:

然后你应该看到:

ruby-1.8.7-p174 > date = "03/17/81".to_date
ruby-1.8.7-p174 > date.to_s(:short_date)
#=> "1981/03/17"

#3


1  

Try Chronic (gem install chronic):

尝试Chronic(宝石安装慢性):

require 'rubygems'
require 'chronic'
puts Chronic.parse "03/17/81"
#=> Tue Mar 17 12:00:00 -0800 1981

Returns a Time object.

返回Time对象。

#4


0  

Don't change Rails, change your code. I don't see any problem in creating a method that converts the string into a suitable date.

不要更改Rails,更改代码。我没有看到创建将字符串转换为合适日期的方法的任何问题。

string = "03/17/81"
date   = if string =~ %r{(\d+)/(\d+)/(\d+)}
  Date.parse("19#{$3}-#{$1}-#{$2}")
else
  nil # failover
end