Forum: Ruby Convert String object to Time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-29 23:14
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")
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-29 23:39
(Received via mailing list)
On Dec 29, 2008, at 5:13 PM, Mmcolli00 Mom wrote:

>
> 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
Rob@AgileConsultingLLC.com
04952a6ee948f345e9c3727850d09a1b?d=identicon&s=25 Dejan Dimic (Guest)
on 2008-12-30 00:07
(Received via mailing list)
On Dec 29, 11:37 pm, Rob Biedenharn <R...@AgileConsultingLLC.com>
wrote:
> > servertime = "13:05"
> You want to look at Time.parse
>
> servertime = Time.parse("13:05")
>
> -Rob
>
> Rob Biedenharn          http://agileconsultingllc.com
> R...@AgileConsultingLLC.com

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 ;-)
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-30 03:00
Thanks both of you! This was very helpful! -MC
This topic is locked and can not be replied to.