将日期格式化为Rails中的单词 [英] Formatting a Date as words in Rails

查看:131
本文介绍了将日期格式化为Rails中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个具有datetime属性的模型实例。我正在使用以下方式显示它:

 <%= @ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆