Ruby中對時間的使用(顯示格式)
阿新 • • 發佈:2019-02-16
標誌 描述
%Y Year (four digits)
%y Year (last two digits)
%b, %B Short month, full month
%m Month (number)
%d Day of month (left-padded with zeros)
%e Day of month (left-padded with blanks)
%a, %A Short day name, full day name
%H,%I Hour (24-hour clock), hour (12-hour clock)
%M Minute
%S Second
%c Equivalent to "%a %b %d %H:%M:%S %Y"
%x Equivalent to "%m/%d/%y"
例項:
[code]
irb(main):001:0> t=Time.new
=> Wed Nov 08 16:20:19 +0800 2006
irb(main):002:0> t.strftime("Today is %x")
=> "Today is 11/08/06"
irb(main):003:0> t.strftime("Otherwise kown as %d-%b-%y")
=> "Otherwise kown as 08-Nov-06"
irb(main):004:0> t.strftime("Or even day %e of %B,%Y.")
=> "Or even day of November,2006."
irb(main):005:0> t.strftime("The time is %H:%m")
=> "The time is 16:11"
irb(main):006:0> t.year
=> 2006
irb(main):008:0> t.month
=> 11
irb(main):009:0> t.day
=> 8
irb(main):010:0> t.hour
=> 16
irb(main):011:0> t.min
=> 20
irb(main):012:0> t.sec
=> 19
irb(main):013:0> t.usec
=> 984000[/code]
參考:Ruby for Rails
%Y Year (four digits)
%y Year (last two digits)
%b, %B Short month, full month
%m Month (number)
%d Day of month (left-padded with zeros)
%e Day of month (left-padded with blanks)
%a, %A Short day name, full day name
%H,%I Hour (24-hour clock), hour (12-hour clock)
%M Minute
%S Second
%c Equivalent to "%a %b %d %H:%M:%S %Y"
%x Equivalent to "%m/%d/%y"
例項:
[code]
irb(main):001:0> t=Time.new
=> Wed Nov 08 16:20:19 +0800 2006
irb(main):002:0> t.strftime("Today is %x")
=> "Today is 11/08/06"
irb(main):003:0> t.strftime("Otherwise kown as %d-%b-%y")
=> "Otherwise kown as 08-Nov-06"
irb(main):004:0> t.strftime("Or even day %e of %B,%Y.")
=> "Or even day of November,2006."
irb(main):005:0> t.strftime("The time is %H:%m")
=> "The time is 16:11"
irb(main):006:0> t.year
=> 2006
irb(main):008:0> t.month
=> 11
irb(main):009:0> t.day
=> 8
irb(main):010:0> t.hour
=> 16
irb(main):011:0> t.min
=> 20
irb(main):012:0> t.sec
=> 19
irb(main):013:0> t.usec
=> 984000[/code]
參考:Ruby for Rails