如何在 ruby​​ 中格式化日期以包含“rd” 如“第三”中所示。

发布于 2024-07-26 07:27:25 字数 97 浏览 6 评论 0 原文

我想格式化日期对象,以便可以显示“7 月 3 日”或“10 月 1 日”等字符串。 我在 Date.strftime 中找不到生成“rd”和“st”的选项。 有人知道怎么做吗?

I want to format a date object so that I can display strings such as "3rd July" or "1st October". I can't find an option in Date.strftime to generate the "rd" and "st". Any one know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷清清 2024-08-02 07:27:26

我会同意其他人的观点,但我只是鼓励您下载 activesupport gem,这样您就可以将它用作库。 您不需要所有 Rails 都可以使用 ordinalize

% gem install activesupport
...
% irb 
irb> require 'rubygems'
#=>  true
irb> require 'activesupport'
#=>  true
irb> 3.ordinalize
#=>  "3rd"

I'm going to echo everyone else, but I'll just encourage you to download the activesupport gem, so you can just use it as a library. You don't need all of Rails to use ordinalize.

% gem install activesupport
...
% irb 
irb> require 'rubygems'
#=>  true
irb> require 'activesupport'
#=>  true
irb> 3.ordinalize
#=>  "3rd"
a√萤火虫的光℡ 2024-08-02 07:27:26

我不认为 Ruby 有它,但如果你有 Rails,试试这个:-

puts 3.ordinalize #=> "3rd"

I don't think Ruby has it, but if you have Rails, try this:-

puts 3.ordinalize #=> "3rd"
一杯敬自由 2024-08-02 07:27:26

似乎我正在第三次重新讨论这个主题,所以我用一些额外的评论/用法更新了我的要点。

https://gist.github.com/alterisian/4154152

干杯,
伊恩.

Seems I'm revisiting this topic for a third time, so I've updated my gist with some extra comments / usage.

https://gist.github.com/alterisian/4154152

Cheers,
Ian.

欢烬 2024-08-02 07:27:26

我不知道它是否比 switch-case 快得多(任何?),但我用结尾做了一个常量:

DAY_ENDINGS = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"]

然后就这样使用它:

DAY_ENDINGS[date.mday]

因为我想要结尾在 a 中

<span>th</span>

I don't know if it makes it that much (any?) faster than switch-case, but I made a constant with the endings:

DAY_ENDINGS = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st"]

Then just used it like:

DAY_ENDINGS[date.mday]

As I wanted the ending inside a

<span>th</span>
我们只是彼此的过ke 2024-08-02 07:27:26

需要“主动支持”
1.序数=> 'st'
1.排序=> “第一”

require 'activesupport'
1.ordinal => 'st'
1.ordinalize => '1st'

潇烟暮雨 2024-08-02 07:27:26
require 'time'
H = Hash.new do |_,k|
  k +
  case k
  when '1', '21', '31'
    'st'
  when '2', '22'
    'nd'
  when '3', '23'
    'rd'
  else
    'th'
  end
end 
def fmt_it(time)
  time.strftime("%A %-d, %-l:%M%P").sub(/\d+(?=,)/, H) 
end
fmt_it(Time.new)
  #=> "Wednesday 9th, 1:36pm"
fmt_it(Time.new + 3*24*60*60)
  #=> "Saturday 12th, 3:15pm"

我使用了 String#sub (可以使用 sub!),它采用哈希 (H) 作为第二个参数。

sub 使用的正则表达式为“匹配一个或多个数字,后跟一个逗号”。 (?=,) 是一个正向前瞻

我使用 H rel="nofollow noreferrer">Hash::new 占用一个块。 这仅意味着如果 H 没有键 k,则 H[k] 返回由块计算的值。 在这种情况下,哈希值是空的,因此块总是返回感兴趣的值。 该块采用两个参数,哈希值(此处为 H)和正在评估的密钥。 我用下划线表示前者,表明该块不使用它)。 一些示例:

H['1']  #=>  "1st" 
H['2']  #=>  "2nd" 
H['3']  #=>  "3rd" 
H['4']  #=>  "4th" 
H['9']  #=>  "9th" 
H['10'] #=> "10th" 
H['11'] #=> "11th" 
H['12'] #=> "12th" 
H['13'] #=> "13th" 
H['14'] #=> "14th" 
H['22'] #=> "22nd" 
H['24'] #=> "24th" 
H['31'] #=> "31st" 

请参阅 Time#strftime 了解格式化指令。

require 'time'
H = Hash.new do |_,k|
  k +
  case k
  when '1', '21', '31'
    'st'
  when '2', '22'
    'nd'
  when '3', '23'
    'rd'
  else
    'th'
  end
end 
def fmt_it(time)
  time.strftime("%A %-d, %-l:%M%P").sub(/\d+(?=,)/, H) 
end
fmt_it(Time.new)
  #=> "Wednesday 9th, 1:36pm"
fmt_it(Time.new + 3*24*60*60)
  #=> "Saturday 12th, 3:15pm"

I have used the form of String#sub (one could use sub!) that takes a hash (H) as its second argument.

The regular expression used by sub reads "match one or more digits followed by a comma". (?=,) is a positive lookahead.

I created the (empty) hash H using the form of Hash::new that takes a block. This simply means that if H does not have a key k, H[k] returns the value computed by the block. In this case the hash is empty so the block always returns the value of interest. The block takes two arguments, the hash (here H) and the key being evaluated. I've represented the former with an underscore, signalling that it is not used by the block). Some examples:

H['1']  #=>  "1st" 
H['2']  #=>  "2nd" 
H['3']  #=>  "3rd" 
H['4']  #=>  "4th" 
H['9']  #=>  "9th" 
H['10'] #=> "10th" 
H['11'] #=> "11th" 
H['12'] #=> "12th" 
H['13'] #=> "13th" 
H['14'] #=> "14th" 
H['22'] #=> "22nd" 
H['24'] #=> "24th" 
H['31'] #=> "31st" 

See Time#strftime for formatting directives.

单调的奢华 2024-08-02 07:27:25

除非你使用Rails,否则添加这个ordinalize方法(无耻地编码
从 Rails 源代码中提取)到 Fixnum

class Fixnum
  def ordinalize
    if (11..13).include?(self % 100)
      "#{self}th"
    else
      case self % 10
        when 1; "#{self}st"
        when 2; "#{self}nd"
        when 3; "#{self}rd"
        else    "#{self}th"
      end
    end
  end
end

然后将日期格式设置如下:

> now = Time.now
> puts now.strftime("#{now.day.ordinalize} of %B, %Y")
=> 4th of July, 2009

Unless you're using Rails, add this ordinalize method (code shamelessly
lifted from the Rails source) to the Fixnum class

class Fixnum
  def ordinalize
    if (11..13).include?(self % 100)
      "#{self}th"
    else
      case self % 10
        when 1; "#{self}st"
        when 2; "#{self}nd"
        when 3; "#{self}rd"
        else    "#{self}th"
      end
    end
  end
end

Then format your date like this:

> now = Time.now
> puts now.strftime("#{now.day.ordinalize} of %B, %Y")
=> 4th of July, 2009
久伴你 2024-08-02 07:27:25
created_at.strftime("#{created_at.day.ordinalize} of %m, %y")

将制作《2009年7月4日》

created_at.strftime("#{created_at.day.ordinalize} of %m, %y")

Will produce "4th of July, 2009"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文