将字符串解析为DateTime对象并在Ruby中添加分钟

将字符串解析为DateTime对象并在Ruby中添加分钟,ruby,datetime,Ruby,Datetime,我不知道我做错了什么。我遵循了无数的例子,但我无法解决这个问题。我有一个包含以下时间的字符串: 文本=东部时间下午1:00 我尝试将其转换为DateTime对象,以便使用以下代码轻松地为值添加30分钟: text_next = DateTime.strptime(text_t, '%I:%M %p %Z') puts text_next text_next = text_next + 1800 puts text_next 但我的输出如下所示: 1:00 PM ET 2013-

我不知道我做错了什么。我遵循了无数的例子,但我无法解决这个问题。我有一个包含以下时间的字符串:

文本=东部时间下午1:00

我尝试将其转换为DateTime对象,以便使用以下代码轻松地为值添加30分钟:

  text_next = DateTime.strptime(text_t, '%I:%M %p %Z')
  puts text_next
  text_next = text_next + 1800
  puts text_next
但我的输出如下所示:

1:00 PM ET
2013-07-02T13:00:00+00:00
2018-06-06T13:00:00+00:00

我需要将时间增加30分钟,然后将其转换回与原来格式相同的字符串。我可以做一个函数来操作字符串,使其提前30分钟,但我觉得这是一个很大的工作,必须有一些东西赋予这个功能

怎么样?当然,只要您使用的是activesupport

text_t = "1:00 PM ET"
text_next = DateTime.strptime(text_t, '%I:%M %p %Z')
text_next = text_next + 30.minutes

那怎么办?当然,只要您使用的是activesupport

text_t = "1:00 PM ET"
text_next = DateTime.strptime(text_t, '%I:%M %p %Z')
text_next = text_next + 30.minutes

您的日期将增加1800天

我试过这个:

text_next = DateTime.strptime(text_t, '%I:%M %p %Z')
puts text_next
text_next = text_next + Rational(30, 1440)
puts text_next

1440是一天中的分钟数

您的日期将增加1800天

我试过这个:

text_next = DateTime.strptime(text_t, '%I:%M %p %Z')
puts text_next
text_next = text_next + Rational(30, 1440)
puts text_next

1440是一天中的分钟数

以下是我将遵循的方法:

require 'date'

text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00 ((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00" # !> invalid offset is ignored
dh = Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
#     :mon=>7,
#     :mday=>3,
#     :hour=>14,
#     :min=>12,
#     :sec=>3,
#     :zone=>"+00:00",
#     :offset=>0}

dh[:hour] += 30 # => 44
dh
# => {:year=>2013,
#     :mon=>7,
#     :mday=>3,
#     :hour=>44,
#     :min=>12,
#     :sec=>3,
#     :zone=>"+00:00",
#     :offset=>0}

dh.values[0..-2].join(" ")
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #<DateTime: 2013-01-07T03:44:12+00:00 ((2456300j,13452s,0n),+0s,2299161j)>
需要“日期”
text_t='东部时间下午2:12:03'
dt=DateTime.parse(文本,“%I:%M%p%Z”)
# => #
杜邦
#=>“2013-07-03T14:12:03+00:00”#!>忽略无效的偏移量
dh=日期._strTime(dt.to_s,%Y-%m-%dt%H:%m:%s%z')
#=>{:年份=>2013年,
#:mon=>7,
#:mday=>3,
#:小时=>14,
#:min=>12,
#:秒=>3,
#:zone=>“+00:00”,
#:偏移量=>0}
dh[:小时]+=30#=>44
dh
#=>{:年份=>2013年,
#:mon=>7,
#:mday=>3,
#:小时=>44,
#:min=>12,
#:秒=>3,
#:zone=>“+00:00”,
#:偏移量=>0}
dh.values[0..-2]。连接(“”)
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #

现在,您可以选择查看日期对象的方式。

以下是我将遵循的方法:

require 'date'

text_t = '2:12:03 PM ET'
dt = DateTime.parse(text_t, '%I:%M %p %Z')
# => #<DateTime: 2013-07-03T14:12:03+00:00 ((2456477j,51123s,0n),+0s,2299161j)>
dt.to_s
# => "2013-07-03T14:12:03+00:00" # !> invalid offset is ignored
dh = Date._strptime(dt.to_s,'%Y-%m-%dT%H:%M:%S%z')
# => {:year=>2013,
#     :mon=>7,
#     :mday=>3,
#     :hour=>14,
#     :min=>12,
#     :sec=>3,
#     :zone=>"+00:00",
#     :offset=>0}

dh[:hour] += 30 # => 44
dh
# => {:year=>2013,
#     :mon=>7,
#     :mday=>3,
#     :hour=>44,
#     :min=>12,
#     :sec=>3,
#     :zone=>"+00:00",
#     :offset=>0}

dh.values[0..-2].join(" ")
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #<DateTime: 2013-01-07T03:44:12+00:00 ((2456300j,13452s,0n),+0s,2299161j)>
需要“日期”
text_t='东部时间下午2:12:03'
dt=DateTime.parse(文本,“%I:%M%p%Z”)
# => #
杜邦
#=>“2013-07-03T14:12:03+00:00”#!>忽略无效的偏移量
dh=日期._strTime(dt.to_s,%Y-%m-%dt%H:%m:%s%z')
#=>{:年份=>2013年,
#:mon=>7,
#:mday=>3,
#:小时=>14,
#:min=>12,
#:秒=>3,
#:zone=>“+00:00”,
#:偏移量=>0}
dh[:小时]+=30#=>44
dh
#=>{:年份=>2013年,
#:mon=>7,
#:mday=>3,
#:小时=>44,
#:min=>12,
#:秒=>3,
#:zone=>“+00:00”,
#:偏移量=>0}
dh.values[0..-2]。连接(“”)
# => "2013 7 3 44 12 3 +00:00"
DateTime.ordinal(*dh.values[0..-3])
# => #

现在,您可以选择如何查看您的
日期
对象。

从我收集的信息来看,rails提供了积极的支持?我没有使用rails。从我收集到的积极支持是在rails中?我没有使用rails。我仍然需要弄清楚如何格式化它,但这是正确增加时间所必需的,谢谢你可以使用字符串插值来实现这一点。text_next.day、text_next.hour、text_next.minute、text_next.second等将为您提供所需内容。不过,另一个问题已经出现。我无法将小时设置为12小时值—它始终是24小时。我也有正确的格式。%我不是%H,它们都返回24小时值。奇怪,我可能会将其作为另一个问题发布。text_next.strftime(“%I”)将为您提供01..12小时格式。%H将给你00..23我仍然需要弄清楚如何格式化它,但这是正确增加时间所必需的,谢谢你可以使用字符串插值来实现这一点。text_next.day、text_next.hour、text_next.minute、text_next.second等将为您提供所需内容。不过,另一个问题已经出现。我无法将小时设置为12小时值—它始终是24小时。我也有正确的格式。%我不是%H,它们都返回24小时值。奇怪,我可能会将其作为另一个问题发布。text_next.strftime(“%I”)将为您提供01..12小时格式。%H将给你00..23