# Convert String object to Time

**URL:** <https://rubytalk.org/t/convert-string-object-to-time/50919>\
**Category:** ruby-talk\
**Created:** [29 December 2008 22:13 UTC](https://rubytalk.org/t/convert-string-object-to-time/50919 "2008-12-29T22:13:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [29 December 2008 22:13 UTC](https://rubytalk.org/t/convert-string-object-to-time/50919/1 "2008-12-29T22:13:45Z")

</div>

Hi

I have a time value coming in as a string from another program and I  
want to convert it to time class and then to regular time attaching AM  
or PM on the end. This is what I have tried.

servertime = "13:05"  
servertime.Time  
servertime.strftime("%p") #\<-this gives a no method error for strftime

I was thinking that if I convert it to time just like using the time  
class demonstarted below that it would work the same for a time value.  
However it doesn't. Do you know what I am doing wrong? Thanks -M

t = Time.now  
t.strftime("I%:%M%p")

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [29 December 2008 22:37 UTC](https://rubytalk.org/t/convert-string-object-to-time/50919/2 "2008-12-29T22:37:57Z")

</div>

You want to look at Time.parse

servertime = Time.parse("13:05")

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 29, 2008, at 5:13 PM, Mmcolli00 Mom wrote:
> 
> > Hi
> > 
> > I have a time value coming in as a string from another program and I  
> > want to convert it to time class and then to regular time attaching AM  
> > or PM on the end. This is what I have tried.
> > 
> > servertime = "13:05"  
> > servertime.Time  
> > servertime.strftime("%p") #\<-this gives a no method error for strftime
> > 
> > I was thinking that if I convert it to time just like using the time  
> > class demonstarted below that it would work the same for a time value.  
> > However it doesn't. Do you know what I am doing wrong? Thanks -M
> > 
> > t = Time.now  
> > t.strftime("I%:%M%p")

---

<div class="post-metadata">

**Author:** ![Dejan\_Dimic](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Dejan\_Dimic](https://rubytalk.org/u/Dejan_Dimic)\
**Post date:** [29 December 2008 23:04 UTC](https://rubytalk.org/t/convert-string-object-to-time/50919/3 "2008-12-29T23:04:39Z")

</div>

servertime = Time.parse("13:05").strftime("%I:%M %p")

P.S.  
Never trust the user input so be prepared for possible exceptions on  
parsing the time string from server 😉

> **···**
>
> On Dec 29, 11:37 pm, Rob Biedenharn \<R...@AgileConsultingLLC.com\> wrote:
> 
> > On Dec 29, 2008, at 5:13 PM, Mmcolli00 Mom wrote:
> > 
> > \> Hi
> > 
> > \> I have a time value coming in as a string from another program and I  
> > \> want to convert it to time class and then to regular time attaching AM  
> > \> or PM on the end. This is what I have tried.
> > 
> > \> servertime = "13:05"  
> > \> servertime.Time  
> > \> servertime.strftime("%p") #\<-this gives a no method error for strftime
> > 
> > \> I was thinking that if I convert it to time just like using the time  
> > \> class demonstarted below that it would work the same for a time value.  
> > \> However it doesn't. Do you know what I am doing wrong? Thanks -M
> > 
> > \> t = Time.now  
> > \> t.strftime("I%:%M%p")
> > 
> > You want to look at Time.parse
> > 
> > servertime = Time.parse("13:05")
> > 
> > -Rob
> > 
> > Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > R...@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [30 December 2008 01:59 UTC](https://rubytalk.org/t/convert-string-object-to-time/50919/4 "2008-12-30T01:59:47Z")

</div>

Thanks both of you! This was very helpful! -MC

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
