rails 中的 时间

rails中的时间显示格式

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

 

strftime Format Codes

Year

Example
%Yyear with century2007
%yyear without century07
%Ccentury number (year divided by 100)20

Month

Example
%Bfull month nameJanuary
%babbreviated month nameJan
%hsame as %bJan
%mmonth as number(01-12)

Week

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

Day

Example
%Afull weekday nameWednesday
%aabbreviated weekday nameWed
%dday of the month(01-31)
%eday of the month, single digits preceded by space( 1-31)
%jday of the year(001-366)
%wweekday as a number, with 0 representing Sunday(0-6)
%uweekday as a number, with 1 representing Monday(1-7)

Time

Example
%Hhour (24-hour clock)(00-23)
%khour (24-hour clock); single digits preceded by space( 0-23)
%Ihour (12-hour clock)(01-12)
%lhour (12-hour clock); single digits preceded by space( 1-12)
%Mminute(00-59)
%Sseconds(00-59)
%peither AM or PMAM
%Ztimezone name or abbreviationEDT
%ztimezone offset from UTC-0400

Summaries

Example
%Ddate, same as %m/%d/%y05/16/07
%vdate, same as %e-%b-%Y16-May-2007
%Fdate, same as %Y-%m-%d2007-05-16
%Rtime, 24 hour notation, same as %H:%M18:06
%Ttime, 24 hour notation, same as %H:%M:%S18:06:15
%rtime, am/pm notation, same as %I:%M:%S %p06:06:15 PM

Formatting

%nnewline character
%ttab character
%%percent character

Less common formats

%snumber of seconds since the Epoch, UTC
%cnational date and time representation
%+national date and time representation
%xnational date representation
%Xnational time representation
%Gyear with century, starting on first Monday where week has 4 or more days.
%gyear without century, starting on first Monday where week has 4 or more days.
%Vweek 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上的时间也是比较全面的。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值