使用Ruby / Rails进行strftime格式化—小写的am / pm

strftime formatting with Ruby/Rails — Lowercase am/pm

我有以下内容:

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

哪些输出:12月19日上午10:32

我想输出一个小写的上午,例如:12月19日上午10:32

关于如何使用ruby / rails做到这一点的任何想法?

谢谢


尝试使用%P代替%p


您可以将它们保存到不同的var中,然后执行以下操作:

1
2
3
4
5
6
<wyn>
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


使用%P代替%p。它将与ruby 1.9一起使用,但对于1.8,则需要使用.sub(' AM ', ' am ').sub(' PM ', ' pm ')或类似名称。


在ruby 1.9中,您可以使用%P代替%p,并且不要忘记删除空格

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


这篇文章或这应该给您您所需要的。您基本上在初始化器中定义了自定义时间格式。

如果要从API中获取它,它们将显示Time类中的默认值