Ruby/Time/strftime
Материал из Wiki.crossplatform.ru
Содержание |
Format time as "%A, %B %d, %Y"
time = Time.gm(2005, 12, 31, 13, 22, 33) word_date = "%A, %B %d, %Y" time.strftime(word_date) # => "Saturday, December 31, 2005"
Format time as "%D"
time = Time.gm(2005, 12, 31, 13, 22, 33) american_date = "%D" time.strftime(american_date) # => "12/31/05"
Format time as "%d/%m/%y"
time = Time.gm(2005, 12, 31, 13, 22, 33) european_date = "%d/%m/%y" time.strftime(european_date) # => "31/12/05"
Format time as "%m/%d/%Y"
time = Time.gm(2005, 12, 31, 13, 22, 33) four_digit_year_date = "%m/%d/%Y" time.strftime(four_digit_year_date) # => "12/31/2005"
Format time as "%m-%d-%Y %H:%M:%S %Z"
time = Time.gm(2005, 12, 31, 13, 22, 33) date_and_time = "%m-%d-%Y %H:%M:%S %Z" time.strftime(date_and_time) # => "12-31-2005 13:22:33 GMT"
Format time as "%m-%d-%Y %I:%M:%S %p"
time = Time.gm(2005, 12, 31, 13, 22, 33) twelve_hour_clock_time = "%m-%d-%Y %I:%M:%S %p" time.strftime(twelve_hour_clock_time) # => "12-31-2005 01:22:33 PM"
strftime("%U") and strftime("%W")
require "date" t1 = Time.local(2002,5,1) d1 = Date.new(2002,5,1) week1a = t1.strftime("%U").to_i # 17 week1b = t1.strftime("%W").to_i # 17 week1c = d1.cweek # 18 t2 = Time.local(2005,5,1) d2 = Date.new(2005,5,1) week2a = t2.strftime("%U").to_i # 18 week2b = t2.strftime("%W").to_i # 18 week2c = d2.cweek # 17
Time.gm(2006).strftime("The year is %Y!")
puts Time.gm(2006).strftime("The year is %Y!") # => "The year is 2006!"
Time#strftime directives
Format Meaning %a The abbreviated weekday name ("Sun") %A The full weekday name ("Sunday") %b The abbreviated month name ("Jan") %B The full month name ("January") %c The preferred local date and time representation %d Day of the month (01..31) %H Hour of the day, 24 hour clock (00..23) %I Hour of the day, 12 hour clock (01..12) %j Day of the year (001..366) %m Month of the year (01..12) %M Minute of the hour (00..59) %p Meridian indicator ("AM" or "PM") %S Second of the minute (00..60) %U Week number of the current year, starting with thefirst Sunday as the first day of thefirst week (00..53) %W Week number of the current year, starting with thefirst Monday as the first day of thefirst week (00..53) %w Day of the week (Sunday is 0,0..6) %x Preferred representation for the date alone, no time %X Preferred representation for the time alone, no date %y Year without a century (00..99) %Y Year with century %Z Time zone name %% Literal % character