Convert String object to Time

M

Mmcolli00 Mom

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")
 
R

Rob Biedenharn

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
(e-mail address removed)
 
D

Dejan Dimic

You want to look at Time.parse

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

-Rob

Rob Biedenharn          http://agileconsultingllc.com
(e-mail address removed)

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 ;-)
 

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

No members online now.

Forum statistics

Threads
473,985
Messages
2,570,199
Members
46,766
Latest member
rignpype

Latest Threads

Top