将日期格式化为Rails中的单词 [英] Formatting a Date as words in Rails
本文介绍了将日期格式化为Rails中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%= @ instance.date.to_date%>
但它显示为:2011-09-09
我想要显示为:2011年9月9日
我该怎么做?
谢谢!
解决方案
<%= @ instance.date.strftime %B%d,%Y)%>
这将给你2011年9月9日。
如果你真的需要一天的序数(th,rd等),你可以这样做:
code><%= @ instance.date.strftime(%B #{@instance.date.day.ordinalize},%Y)%>
So I have a model instance that has a datetime attribute. I am displaying it in my view using:
<%= @instance.date.to_date %>
but it shows up as: 2011-09-09
I want it to show up as: September 9th, 2011
How do I do this?
Thanks!
解决方案
<%= @instance.date.strftime("%B %d, %Y") %>
This would give you "September 9, 2011".
If you really needed the ordinal on the day ("th", "rd", etc.), you could do:
<%= @instance.date.strftime("%B #{@instance.date.day.ordinalize}, %Y") %>
这篇关于将日期格式化为Rails中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文