在Ruby on Rails中,如何使用“th”格式格式化日期,后缀,如“Sun Oct 5th” [英] In Ruby on Rails, how do I format a date with the "th" suffix, as in, "Sun Oct 5th"?

查看:184
本文介绍了在Ruby on Rails中,如何使用“th”格式格式化日期,后缀,如“Sun Oct 5th”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示以下格式的日期:短的星期几,短的月份,月的日子,而不包括前导零,但包括th,st,nd或rd后缀。

I want to display dates in the format: short day of week, short month, day of month without leading zero but including "th", "st", "nd", or "rd" suffix.

例如,这个问题的日期将显示Thu Oct 2nd。

For example, the day this question was asked would display "Thu Oct 2nd".

我使用的是Ruby 1.8。 7, Time.strftime 似乎不行这个。

I'm using Ruby 1.8.7, and Time.strftime just doesn't seem to do this. I'd prefer a standard library if one exists.

推荐答案

使用active_support中的ordinalize方法。

Use the ordinalize method from 'active_support'.

>> time = Time.new
=> Fri Oct 03 01:24:48 +0100 2008
>> time.strftime("%a %b #{time.day.ordinalize}")
=> "Fri Oct 3rd"

请注意,如果您使用IRB与Ruby 2.0,则必须首先运行:

Note, if you are using IRB with Ruby 2.0, you must first run:

require 'active_support/core_ext/integer/inflections'

这篇关于在Ruby on Rails中,如何使用“th”格式格式化日期,后缀,如“Sun Oct 5th”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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