Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby DateTime.Parse to local time

Tags:

datetime

ruby

I have a date string 20101129220021, so I will use

require 'date'
d = DateTime.parse('20101129220021')

This part works fine, and I get a date, which is in UTC.

My question is, how can I convert this into my local time? I tried many methods like extracting the time part using d.to_time and manipulate the result, but it didn't work. As far as I know, DateTime object is immutable. Can I please get some help?

like image 540
drhanlau Avatar asked May 07 '11 12:05

drhanlau


1 Answers

irb(main):001:0> require "date"
=> true
irb(main):002:0> d = DateTime.parse('20101129220021')
=> #<DateTime: 2010-11-29T22:00:21+00:00 (70719276007/28800,0/1,2299161)>
irb(main):003:0> d.to_time
=> 2010-11-30 00:00:21 +0200

ruby 1.9.2p180 (2011-02-18)

like image 71
Vasiliy Ermolovich Avatar answered Oct 10 '22 03:10

Vasiliy Ermolovich