ITeye博客

rails中的时间显示格式【转】

clark1231 · 2013-02-26 12:21 · rails · 互联网 · Markdown

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

strftime Format Codes

Year   Example %Y year with century 2010 %y year without century 10 %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.

 

rails 实现 动态显示时间

2010-12-17 16:25

视图:  

<%= javascript_include_tag :defaults %>
 
<%= periodically_call_remote :url => new_ajax_fun_path, :update =>'result', :frequency => 1 %>
 
 <div id="result"></div>

  

控制器: 

def new
    respond_to do |wants|
      wants.js {render :text => Time.now.strftime("%Y-%m-%d %H:%M:%S")}
    end
end

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。