# Convert String to Time object

**URL:** <https://www.ruby-forum.com/t/convert-string-to-time-object/109738>\
**Category:** Ruby\
**Created:** [July 18, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/convert-string-to-time-object/109738 "2007-07-18T14:40:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/convert-string-to-time-object/109738/1 "2007-07-18T14:40:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/convert-string-to-time-object/109738/2 "2007-07-18T14:47:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/convert-string-to-time-object/109738/3 "2007-07-18T15:04:51Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 18, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/convert-string-to-time-object/109738/4 "2007-07-18T15:11:29Z")

</div>

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/](http://chronic.rubyforge.org/)

best,  
Dan
