Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails getting the current time without the date

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!

like image 686
pka Avatar asked Jun 29 '12 13:06

pka


1 Answers

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"
like image 174
meagar Avatar answered Sep 20 '22 00:09

meagar