为人类设定日期和时间格式。
gem install stamp,或者将 stamp 添加到 Gemfile 中并执行 bundle install。 ## 使用 Ruby 日期和时间获得了一个强大的新方法: stamp。 您可能担心"stamp"对于不熟悉此 gem 的开发人员阅读您的代码时不够描述性。如果是这样,则提供了以下别名: * stamp_like * format_like ### 日期 给 Date#stamp 提供一个带有您想要的月份、日期、年份和星期几部分的示例日期字符串,日期将会相应地被格式化: ```rubydate = Date.new(2011, 6, 9) date.stamp("March 1, 1999") #=> "June 9, 2011" date.stamp("Jan 1, 1999") #=> "Jun 9, 2011" date.stamp("Jan 01") #=> "Jun 09" date.stamp("Sunday, May 1, 2000") #=> "Thursday, June 9, 2011" date.stamp("Sun Aug 5") #=> "Thu Jun 9" date.stamp("12/31/99") #=> "06/09/11" date.stamp("DOB: 12/31/2000") #=> "DOB: 06/09/2011"
```ruby
date.stamp("November 5th") #=> "June 9th"
date.stamp("1st of Jan") #=> "9th of Jun"Time#stamp 支持与 Date 相同的示例类型,但还支持格式化
当它看到用冒号分隔的值时,会显示时、分和秒。
time = Time.utc(2011, 6, 9, 20, 52, 30)
time.stamp("3:00 AM") #=> "8:52 PM"
time.stamp("01:00:00 AM") #=> "08:52:30 PM"
time.stamp("23:59") #=> "20:52"
time.stamp("23:59:59") #=> "20:52:30"
time.stamp("Jan 1 at 01:00 AM") #=> "Jun 9 at 08:52 PM"
time.stamp("23:59 UTC") #=> "20:52 PST"也支持 UTC 时区偏移,支持 +HHMM 和 +HH:MM 两种格式:
time.stamp("23:59 +00:00") #=> "20:52 -05:00"如果您不想修补 Date 和 Time, 可以使用模块函数:
require "stamp/core" # instead of "stamp"
Stamp.format(Date.new(2011, 6, 9), "Jan 1, 1999") #=> "Jun 9, 2011"或者针对选择参与的文件进行优化:
require "stamp/refin"暂无开放 Issues,或尚未同步最近议题。