Rails 日期扩展

[quote]now = Time.now[/quote]
puts now   #=>   Thu May 18 23:36:10 CDT 2006

puts now.to_date   #=>   2006-05-18

puts now.to_s   #=>   Thu May 18 23:36:10 CDT 2006

puts now.to_s(:short)   #=>   18 May 23:36

puts now.to_s(:long)   #=>   May 18, 2006 23:36

puts now.to_s(:db)   #=>   2006-05-18 23:36:10

puts now.to_s(:rfc822)   #=> Thu, 18 May 2006 23:36:10 -0500

puts now.ago(3600)   #=> Thu May 18 22:36:10 CDT 2006

puts now.at_beginning_of_day   #=> Thu May 18 00:00:00 CDT 2006

puts now.at_beginning_of_month   #=> Mon May 01 00:00:00 CDT 2006

puts now.at_beginning_of_week   #=> Mon May 15 00:00:00 CDT 2006

puts now.at_beginning_of_quarter   #=> Sat Apr 01 00:00:00 CST 2006

puts now.at_beginning_of_year   #=> Sun Jan 01 00:00:00 CST 2006

puts now.at_midnight   #=> Thu May 18 00:00:00 CDT 2006

puts now.change(:hour => 13)   #=> Thu May 18 13:00:00 CDT 2006

puts now.last_month   #=> Tue Apr 18 23:36:10 CDT 2006

puts now.last_year   #=> Wed May 18 23:36:10 CDT 2005

puts now.midnight   #=> Thu May 18 00:00:00 CDT 2006

puts now.monday   #=> Mon May 15 00:00:00 CDT 2006

puts now.months_ago(2)   #=> Sat Mar 18 23:36:10 CST 2006

puts now.months_since(2)   #=> Tue Jul 18 23:36:10 CDT 2006

puts now.next_week   #=> Mon May 22 00:00:00 CDT 2006

puts now.next_year   #=> Fri May 18 23:36:10 CDT 2007

puts now.seconds_since_midnight   #=> 84970.423472

puts now.since(7200)   #=> Fri May 19 01:36:10 CDT 2006

puts now.tomorrow   #=> Fri May 19 23:36:10 CDT 2006

puts now.years_ago(2)   #=> Tue May 18 23:36:10 CDT 2004

puts now.years_since(2)   #=> Sun May 18 23:36:10 CDT 2008

puts now.yesterday   #=> Wed May 17 23:36:10 CDT 2006

puts now.advance(:days => 30)   #=> Sat Jun 17 23:36:10 CDT 2006

puts Time.days_in_month(2)   #=> 28

puts Time.days_in_month(2, 2000)   #=> 29

puts now.xmlschema   #=> "2006-05-18T23:36:10-06:00"


Rail 2.1 支持时区。可以在 [quote]config/environment.rb[/quote] 中设置默认时区:
config.time_zone = 'UTC'

在 action 中,你可以覆盖默认时区,并把时间转换到指定时区。
Time.zone = 'Eastern Time (US & Canada)'
puts Time.now.in_time_zone

Rails 2.2 引入了 past?、today?和 future? 三个方法用来判断给定时间是在当前时间之前、
和现在是同一天还是在当前时间之后

Date 对象也增加了几个有用的方法。
[quote]date = Date.today[/quote]
puts date.tomorrow #=> "Fri, 19 May 2006"

puts date.yesterday #=> "Wed, 17 May 2006"

puts date.current #=> "Thu, 18 May 2006"



如果设置了 config.time_zone,current()返回 Time.zone.today,
否则返回 Date.today:

puts date.to_s   #=>   "2006-05-18"

puts date.xmlschema   #=>   "2006-05-18T00:00:00-06:00"

puts date.to_time   #=>   Thu May 18 00:00:00 CDT 2006

puts date.to_s(:short)   #=>   "18 May"

puts date.to_s(:long)   #=>   "May 18, 2006"

puts date.to_s(:db)   #=>   "2006-05-18"



你还可以扩展日期对象的 to_s 方法,
将上述格式化逻辑封装起来。
只要在 [quote]environment.rb[/quote]
文件中加上下列代码:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:chatty => "It's %I:%M%p on %A, %B %d, %Y"
)

现在你就可以这样编写代码:
any_date.to_s(:ordinal)   #=> "2006-149"

同样,也可以扩展 Time 类的格式化逻辑。
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:chatty => "It's %I:%M%p on %A, %B %d, %Y"
)

Time.now.to_s(:chatty) #=> "It's 12:49PM on Monday, May 29, 2006"

String 类还有两个与时间相关的方法: to_time()和 to_date()方法分别返回 Time 对象和
Date 对象。
puts "2006-12-25 12:34:56".to_time   #=> Mon Dec 25 12:34:56 UTC 2006

puts "2006-12-25 12:34:56".to_date   #=> 2006-12-25


rails 时间格式化参数

%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 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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值