Rails getting the current time without the date

20,092

Solution 1

Use strftime to get the bits of the time that you want, in the format you want them.

puts Time.now.strftime("%I:%M:%S %z") # "09:33:00 -0400"

puts Time.now.strftime("%I:%M:%S %Z %z") # "09:33:00 EDT -0400"

Solution 2

Time.now.strftime("%H:%M:%S") # "18:33:00"

Don't use %I without %p (won't know am/pm)

Share:
20,092
pka
Author by

pka

Updated on September 02, 2020

Comments

  • pka
    pka over 3 years

    I know there are a lot of questions regarding date and time for Rails, but I can't seem to solve the problem I have.

    I'm probably just being stupid about this.. anyway, my problem is that I want to get the current time without the date attached.

    For example, when I use the following I get:

    Time.now => 2012-06-29 09:23:04 -0400
    

    or

    Time.current => Fri, 29 Jun 2012 09:23:23 EDT -04:00
    

    I would like to obtain the current time without having the date. i.e. just

    09:23:04 -0400 
    

    or

    09:23:23 EDT -04:00
    

    If there are questions out there that already addresses this, I'd appreciate being pointed to them.

    Thanks!

  • x-yuri
    x-yuri about 7 years
    to_formatted_s seems like a better option.