Ruby 时间与日期
Ruby 使用 Time 表示具体时间点,使用标准库 date 中的 Date 表示日期。时间处理看似简单,但涉及时区、格式和业务规则时很容易出错。
Time
now = Time.now
puts now
puts now.year
puts now.month
puts now.day
puts now.hour
puts now.min
puts now.sec
Time.now 返回当前时间。
格式化时间
now = Time.now
puts now.strftime("%Y-%m-%d %H:%M:%S")
常见格式:
%Y:四位年份%m:两位月份%d:两位日期%H:小时%M:分钟%S:秒