`
wellee
  • 浏览: 101366 次
  • 性别: Icon_minigender_1
  • 来自: 温州
社区版块
存档分类
最新评论

rails 中的 时间

阅读更多

rails中的时间显示格式

在rails中需要显示时间时,格式化时间的方法一般选用strftime,下面是关于strftime的一些介绍

 

strftime Format Codes

Year

Example
%Y year with century 2007
%y year without century 07
%C century number (year divided by 100) 20

Month

Example
%B full month name January
%b abbreviated month name Jan
%h same as %b Jan
%m month as number (01-12)

Week

Example
%U week number of the year, Sunday as first day of week (00-53)
%W week number of the year, Monday as first day of week (00-53)

Day

Example
%A full weekday name Wednesday
%a abbreviated weekday name Wed
%d day of the month (01-31)
%e day of the month, single digits preceded by space ( 1-31)
%j day of the year (001-366)
%w weekday as a number, with 0 representing Sunday (0-6)
%u weekday as a number, with 1 representing Monday (1-7)

Time

Example
%H hour (24-hour clock) (00-23)
%k hour (24-hour clock); single digits preceded by space ( 0-23)
%I hour (12-hour clock) (01-12)
%l hour (12-hour clock); single digits preceded by space ( 1-12)
%M minute (00-59)
%S seconds (00-59)
%p either AM or PM AM
%Z timezone name or abbreviation EDT
%z timezone offset from UTC -0400

Summaries

Example
%D date, same as %m/%d/%y 05/16/07
%v date, same as %e-%b-%Y 16-May-2007
%F date, same as %Y-%m-%d 2007-05-16
%R time, 24 hour notation, same as %H:%M 18:06
%T time, 24 hour notation, same as %H:%M:%S 18:06:15
%r time, am/pm notation, same as %I:%M:%S %p 06:06:15 PM

Formatting

%n newline character
%t tab character
%% percent character

Less common formats

%s number of seconds since the Epoch, UTC
%c national date and time representation
%+ national date and time representation
%x national date representation
%X national time representation
%G year with century, starting on first Monday where week has 4 or more days.
%g year without century, starting on first Monday where week has 4 or more days.
%V week number of the year, starting on first Monday where week has 4 or more days.

 

 

 

---------------------------------------------------------------------------------------------

格式化时间显示

<%= time.to_s(:lang)%>
=>May 15, 2007 08:00
 
<%= time.to_s(:short)%>
=>15, May 08:00
 
<%= time.to_s(:db)%>
=>2007-05-15 08:00:00

------------------------------------------------------------------------
     time.strftime( string ) => string
------------------------------------------------------------------------
     Formats _time_ according to the directives in the given format
     string. Any text not listed as a directive will be passed through
     to the output string.
     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)
       %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 the first Sunday as the first
               day of the first week (00..53)
       %W - Week  number  of the current year,
               starting with the first Monday as the first
               day of the first 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
       
t = Time.now
        t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 04/09/2003"
        t.strftime("at %I:%M%p")            #=> "at 08:56AM"
---------------------------------------------------------------------------------------------
当然,Agile web developing with rails 3rd 里P282上的时间也是比较全面的。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics