user1771980
user1771980

Reputation: 205

How to get the month as string in timestamp in Ruby

I need to get the following timestamp format: "December 13, 2012 2:29:44 PM PST"

time_stamp = Time.now
time = time_stamp.strftime("%m %d, %Y %H:%M:%S %p PST")
time # => "12 13, 2012 14:29:44 PM PST"

How do I get the month string in place of integer and also hours corrected to 2 ?

Upvotes: 0

Views: 1077

Answers (3)

Pavel Nikolov
Pavel Nikolov

Reputation: 9541

Take a look at the documentation here http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-strftime

for 12 based hour use %l and for month use %B

time_stamp = Time.now
time = time_stamp.strftime("%B %d, %Y %l:%M:%S %p PST")
time # => "December 13, 2012 2:29:44 PM PST"

Upvotes: 1

iain
iain

Reputation: 16274

The website you are looking for is: http://www.foragoodstrftime.com/

It contains all the options you can pass to strftime and you build your own ones with ease.

Upvotes: 0

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

You really should have tried googling this first. I found this under the first result for ruby date format:

http://www.dzone.com/snippets/date-time-format-ruby

time = time_stamp.strftime("%B %d, %Y %I:%M:%S %p %Z")

 => "December 13, 2012 02:49:18 PM PST" 

Also, you should %Z instead of PST to ensure you're in the right time zone.

Upvotes: 4

Related Questions