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上的时间也是比较全面的。
分享到:
相关推荐
我花了三整天的时间阅读了本书,做了书中所有的示例程序和练习。从头至尾,循序渐进,不要跳着读,这样才能这种从中收益。 享受 Rails 的盛宴吧! ——Derek Sivers (sivers.org),CD Baby 创始人 欢迎学习《Ruby...
我们认为你的时间如此宝贵以至于不应该花费在为新概念伤脑筋上面。《深入浅出Rails》用最新的认知科学和学习理论打造多感官的学习体验,运用适合大脑工作方式的直观的格式编排,而不是令人昏昏欲睡的密密麻麻的文字...
升级Rails的?这是一个很好的时间来重构你的代码
这几天一直在学习使用RoR(Ruby on Rails),想建立一个功能全面一点的LBS(Location Based Service)网站。但是对于我这个RoR的初学者(仅有几天时间)来说,毕竟太复杂了。因此本文试图简化原来的设计思路,抛弃一切...
Rail Rails汇总中的时间序列数据:fire:Rails中的汇总时间数据可与Ahoy和Searchjoy安装配合使用。将此行添加到应用程序的Gemfile中:gem'rollups'对于Rails <6,还添加:gem'activerecord-import'并运行:bundle ...
Rails应用程序的服务器时间标头
我们认为你的时间如此宝贵以至于不应该花费在为新概念伤脑筋上面。《深入浅出Rails》用最新的认知科学和学习理论打造多感官的学习体验,运用适合大脑工作方式的直观的格式编排,而不是令人昏昏欲睡的密密麻麻的文字...
在讲述Ruby知识的过程中,始终从Rails开发实战出发,揭示Rails和Puby之间的微妙关系,阐述Rails自身特有的技术、惯用法和设计理念,并用Rails开发了一个网上音乐店实例。最后还介绍了探索Rails源代码的各种技术,为...
validates_timeliness - ActiveModel和Rails的日期与时间验证插件,持多个ORM和允许自定义日期/时间格式。
在讲述Ruby知识的过程中,始终从Rails开发实战出发,揭示Rails和Puby之间的微妙关系,阐述Rails自身特有的技术、惯用法和设计理念,并用Rails开发了一个网上音乐店实例。最后还介绍了探索Rails源代码的各种技术,为...
rails-timeago提供了一个timeago_tag助手来创建可用于插件的时间标签。 安装 将此行添加到您的应用程序的Gemfile : gem 'rails-timeago' , '~> 2.0' 然后执行: $ bundle 或将其自己安装为: $ gem install ...
Crono是Ruby on Rails的基于时间的后台作业调度程序守护程序(就像Cron一样)。 目的 当前,还没有Ruby Cron for Rails这样的东西。 嗯,有但是它可以在Unix Cron上运行,因此您不能从Ruby对其进行管理。 Crono是纯...
tiny_mce, Ruby on Rails 插件( pre Rails 3.1 ),允许你轻松地将TinyMCE编辑器实现到你的应用程序中 用于 Rails的 TinyMCE因为我没有时间维护它,而且代码库处于不一致状态,所以这个 gem 被折旧 ! 似乎如下笔记。...
Rails性能 一个自托管的工具,用于监视Ruby on Rails应用程序的性能。 这是New Relic APM,Datadog或其他类似服务的简单免费替代品。 它允许您跟踪: 吞吐量报告(请参阅RPM数量(每分钟的请求数)) 平均响应...
local_time - Rails引擎向用户用当地时间格式来显示时间和日期
Rails框架首次提出是在2004年7月,它的研发者是26岁的丹麦人David Heinemeier ...Rails结合了PHP体系的优点(快速开发)和Java体系的优点(程序规整),因此,Rails在其提出后不长的时间里就受到了业内广泛的关注。
SassC :: Rails 我们都喜欢使用Sass,但是对于较大的代码库,编译可能需要很长时间。 该gem将Sass的C实现集成到资产管道中。 在一个较大的项目中,这使编译速度提高了4倍: # Using sassc-rails[1] pry(main)> ...
Rails在日志文件中提供的基准测试信息。 各种有助于基准测试和性能分析的工具。 性能测试是开发周期不可或缺的一部分。 非常重要的一点是,不要让最终用户在页面完全加载之前等待太长时间。 对于任何非平凡的Web...
在面试过程中大约三分之一的时间,但是您对候选人有所感触( :thumbs_up: / :thumbs_down: ),否则请尝试说服自己。 这是为了帮助自己克服自己的偏见。 受访者 不要学习测试。 并非所有这些问题都将被使用,此处将...