Convert String to Time object

Hi all,
I have a problem, I cannot convert string to Time.
I wrote function, but they don’t work:

def string_to_time(string)
return string unless string.is_a?(String)
time_hash = Date._parse(string)
new_time = Time.mktime(time_hash[:year], time_hash[:mon],
time_hash[:mday])
return new_time
end

Please help!

Taras Koval wrote:

Hi all,
I have a problem, I cannot convert string to Time.
I wrote function, but they don’t work:

def string_to_time(string)
return string unless string.is_a?(String)
time_hash = Date._parse(string)
new_time = Time.mktime(time_hash[:year], time_hash[:mon],
time_hash[:mday])
return new_time
end

Please help!

Seems to work ok. What is the problem?

Incidentally, this seems to have the same effect (see the docs for
Time#parse):

def string_to_time(string)
return string unless string.is_a? String
Time.parse(string)
end

Dan

Daniel L. wrote:

time_hash[:mday])

def string_to_time(string)
return string unless string.is_a? String
Time.parse(string)
end

Dan

Try time string like “17/08/2007” or “2007-15-8”

Taras Koval wrote:

Try time string like “17/08/2007” or “2007-15-8”

Good point. If you need more fancy parsing than that, try out the
‘chronic’ gem.
http://chronic.rubyforge.org/

best,
Dan