【问题标题】:Ruby DateTime format: How can I get 1st, 2nd, 3rd, 4th?Ruby DateTime 格式:如何获得第 1、第 2、第 3、第 4?
【发布时间】:2010-09-09 19:16:20
【问题描述】:

首先,DateTime 格式变量似乎没有记录在任何地方,因此对任何可以在 rubydocs 中向我展示此内容的人表示 +1。其次,在查看Date.strftime 功能代码时,我看不到任何可以让我执行以下操作的内容:

Thursday, September 9th 2010

有谁知道这是否可行?

【问题讨论】:

  • 仅在 Ruby 或 ActiveSupport 中?
  • @shingara,如果您再次使用 ActiveSupport 回答,我也会 +1。

标签: ruby-on-rails ruby datetime-format


【解决方案1】:

您可能想take a look here

总结

time = DateTime.now
time.strftime("%A, %B #{time.day.ordinalize} %Y")

请注意,您在纯 Ruby (2.0) 中运行,您需要调用:

require 'active_support/core_ext/integer/inflections'

【讨论】:

    【解决方案2】:

    您可以在 Time 文档中找到所有方法 %xx

    http://ruby-doc.org/core/classes/Time.html#M000298

    所以你只需要制作

    date.strftime('%A, %B %d %Y')
    

    【讨论】:

    • 这不会对当天的数字进行排序(没有“st”、“nd”等)
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多