将字符串转换为datetime ruby on rails

时间:2021-12-03 21:43:36

I know this is basic but I've been struggling for a few hours now and I can't seem to apply one of the many ways there are to convert a string to datetime so I can save it in the database in this format 2018-03-16 00:12:17.555372. Thanks ahead

我知道这是最基本的,但我已经挣扎了几个小时了,我似乎无法应用将字符串转换为datetime的许多方法之一,因此我可以将它保存在数据库中,格式为2018-03-16 00:12:17.555372。谢谢提前

This is the string output in the console.

这是控制台中的字符串输出。

params[:event][:start_date]
"03/28/2018 1:46 AM"

[EDIT] Following some leads I've come up with smething really dirty maybe someone can help refactor I'm supressing AM or PM because I don't know how to parse that I know it's awfull any help is appreciated!

[编辑]根据一些线索,我想出了一些非常脏的东西,也许有人能帮我重构我正在按AM或PM,因为我不知道如何解析它,我知道它是不完整的。

  if !params[:event][:start_date].empty?
    start_date = params[:event][:start_date]
    start_date = start_date.gsub(/[AMP]/, '').squish 
    a = start_date.split('/')
    tmp = a[0]
    a[0] = a[1]
    a[1] = tmp   
    a = a.split(',').join('/')
    start_date = Time.parse(a)
  end

 if !params[:event][:end_date].empty?
   end_date = params[:event][:end_date]
   end_date = end_date.gsub(/[AMP]/, '').squish 
   a = end_date.split('/')
   tmp = a[0]
   a[0] = a[1]
   a[1] = tmp   
   a = a.split(',').join('/')
   end_date = Time.parse(a)
 end

2 个解决方案

#1


3  

You can use DateTime to parse the date from a specific format.

您可以使用DateTime来从特定格式解析日期。

if the format you are looking to parse is "03/28/2018 1:46 AM" then you can do this.

如果要解析的格式是“03/28/2018 1:46 AM”,那么可以这样做。

date = DateTime.strptime('03/28/2018 1:46 AM', '%m/%d/%Y %I:%M %p')

# date to ISO 8601

puts date.to_time
# output: 2018-03-28 07:16:00 +0530

puts date.strftime("%m/%d/%Y")
# output: 03/28/2018

Date formats:

日期格式:

Date (Year, Month, Day):

%Y - Year with century (can be negative, 4 digits at least)
        -0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
        %_m  blank-padded ( 1..12)
        %-m  no-padded (1..12)
%d - Day of the month, zero-padded (01..31)
        %-d  no-padded (1..31)

Time (Hour, Minute, Second, Subsecond):

%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')

%M - Minute of the hour (00..59)

You can refer to all formats here.

您可以参考这里的所有格式。

#2


2  

You can parse it like so in ruby:

你可以像ruby那样解析它:

Parses the given representation of date and time, and creates a DateTime object. This method does not function as a validator.

解析日期和时间的给定表示,并创建一个DateTime对象。此方法不能作为验证器使用。

DateTime.parse('2001-02-03T04:05:06+07:00')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
                          #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>

Not entirely sure if the string you supplied can be parsed, here is the link to the ruby docs on datetimes Docs

不完全确定您提供的字符串是否可以解析,这里是datetimes文档中的ruby文档的链接。

#1


3  

You can use DateTime to parse the date from a specific format.

您可以使用DateTime来从特定格式解析日期。

if the format you are looking to parse is "03/28/2018 1:46 AM" then you can do this.

如果要解析的格式是“03/28/2018 1:46 AM”,那么可以这样做。

date = DateTime.strptime('03/28/2018 1:46 AM', '%m/%d/%Y %I:%M %p')

# date to ISO 8601

puts date.to_time
# output: 2018-03-28 07:16:00 +0530

puts date.strftime("%m/%d/%Y")
# output: 03/28/2018

Date formats:

日期格式:

Date (Year, Month, Day):

%Y - Year with century (can be negative, 4 digits at least)
        -0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
        %_m  blank-padded ( 1..12)
        %-m  no-padded (1..12)
%d - Day of the month, zero-padded (01..31)
        %-d  no-padded (1..31)

Time (Hour, Minute, Second, Subsecond):

%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')

%M - Minute of the hour (00..59)

You can refer to all formats here.

您可以参考这里的所有格式。

#2


2  

You can parse it like so in ruby:

你可以像ruby那样解析它:

Parses the given representation of date and time, and creates a DateTime object. This method does not function as a validator.

解析日期和时间的给定表示,并创建一个DateTime对象。此方法不能作为验证器使用。

DateTime.parse('2001-02-03T04:05:06+07:00')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
                          #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
                          #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>

Not entirely sure if the string you supplied can be parsed, here is the link to the ruby docs on datetimes Docs

不完全确定您提供的字符串是否可以解析,这里是datetimes文档中的ruby文档的链接。