需要在Ruby中转换日期格式的小帮助 [英] Need small help in converting date format in ruby

查看:120
本文介绍了需要在Ruby中转换日期格式的小帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些输出:

Date.today  => Mon, 25 Jun 2012 
Date.today.to_formatted_s(:long_ordinal)  => "June 25th, 2012" 
Date.today.strftime('%A %d, %B')  =>  "Monday 25, June" 

现在我需要输出格式即:

Now I need output in the format ie:

  Monday 25th, June      or       Thrusday, 1st, October

问题是 to_formatted_s strftime 仅适用于日期,两者或它们都返回字符串。如何以我需要的方式获取输出。

Problem is to_formatted_s and strftime apply only on date and both or them return string. How can I get the output in the way I need.

推荐答案

您可以使用 Date :: DATE_FORMATS 添加新的自定义格式和 Integer.ordinalize 以获取日期序号:

You can use Date::DATE_FORMATS to add a new customized format, and Integer.ordinalize to get the day ordinal:

Date::DATE_FORMATS[:month_ordinal] = lambda { |date| 
  date.strftime("%A #{date.day.ordinalize}, %B") 
}

>> Date.today.to_formatted_s(:month_ordinal)
=> "Monday 25th, June"

这篇关于需要在Ruby中转换日期格式的小帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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