From: Mike Stok Date: 2013-07-03T07:33:25+09:00 Subject: Re: How can I add minutes,seconds and hours to a DateTime object? On 2013-07-02, at 3:41 PM, Love U Ruby wrote: > finally I came up with as below : > > require 'date' > > text_t = '2:12:03 PM ET' > dt = DateTime.parse(text_t, '%I:%M %p %Z') > # => # ((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]) > # => # ((2456300j,13452s,0n),+0s,2299161j)> > > Please suggest me if any wrong calculation has been made by me? Please > > Thanks > > -- > Posted via http://www.ruby-forum.com/. It does seem odd that 30 hours after 2013-07-03T14:12:03+00:00 is 2013-01-07T03:44:12+00:00, and you do seem to be doing a lot of work to add 30 hours - looking a the code there is a lot of "noise". If I look at the documentation for DateTime then I see I can use + to add a fractional number of days, http://www.ruby-doc.org/stdlib-2.0/libdoc/date/rdoc/Date.html#method-i-2B , so: ratdog:tmp mike$ pry [1] pry(main)> require 'date' => false [2] pry(main)> dt = DateTime.parse('2:12:03 PM ET', '%I:%M %p %Z') => # [3] pry(main)> dt + (30.0 / 24) # days => # Hope this helps, Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.