12

I have the following:

@comment.created_at.strftime("%I:%M %p %b %d")

Which outputs: 10:32 AM Dec 19

I'd like to output a lowercase am, like: 10:32am Dec 19

Any ideas on how to do that with ruby/rails?

thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

5 Answers5

10

Try to use %P instead using %p

DANDYYeah
  • 675
  • 6
  • 6
2

Use %P instead of %p. That'll work with ruby 1.9 but for 1.8 you'll need to use .sub(' AM ', ' am ').sub(' PM ', ' pm ') or similar.

noodl
  • 17,143
  • 3
  • 57
  • 55
2

You may save them into different vars and do somthing like:


a = @comment.created_at.strftime("%I:%M")
b = @comment.created_at.strftime("%P").downcase
c = @comment.created_at.strftime("%b %d")

e = a+b+" "+c #=> 10:32am Dec 19

Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85
1

it seems that in ruby 1.9 you can use %P instead of %p and don't forget to remove a space

@comment.created_at.strftime("%I:%M%P %b %d") =>10:32am Dec 19

tolbard
  • 1,273
  • 15
  • 20
1

This post or this should give you what you need. YOu basically define a custom Time format within in the initializers.

If you want to have it from the API, they show what the defaults are in the Time class

Community
  • 1
  • 1
DrColossos
  • 12,656
  • 3
  • 46
  • 67