Convert to/from DateTime and Time in Ruby

Readonly picture Readonly · Nov 11, 2008 · Viewed 153.6k times · Source

How do you convert between a DateTime and a Time object in Ruby?

Answer

anshul picture anshul · Nov 11, 2008
require 'time'
require 'date'

t = Time.now
d = DateTime.now

dd = DateTime.parse(t.to_s)
tt = Time.parse(d.to_s)