Convert String to Time object

T

Taras Koval

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!
 
D

Daniel Lucraft

Taras said:
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
 
T

Taras Koval

Daniel said:
Taras said:
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
Try time string like "17/08/2007" or "2007-15-8"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,786
Latest member
EmilioGuru

Latest Threads

Top