我有以下代码:
@comment.created_at.strftime("%I:%M %p %b %d")
输出结果为:10:32 AM Dec 19
我想要输出小写的am,例如:10:32am Dec 19
有没有关于如何在ruby/rails中实现这个功能的想法?
谢谢
我有以下代码:
@comment.created_at.strftime("%I:%M %p %b %d")
输出结果为:10:32 AM Dec 19
我想要输出小写的am,例如:10:32am Dec 19
有没有关于如何在ruby/rails中实现这个功能的想法?
谢谢
尝试使用%P而不是使用%p
使用%P代替%p。这将适用于Ruby 1.9,但对于1.8,您需要使用.sub(' AM ', ' am ').sub(' PM ', ' pm ')
或类似方法。
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
在Ruby 1.9中,似乎可以使用%P代替%p,不要忘记去掉空格。
@comment.created_at.strftime("%I:%M%P %b %d") =>上午10:32 12月19日
<%= RUBY_VERSION %>
应该能给你一个提示 ;-) - noodl