[转]ruby 时间日期处理 -

来自:http://www.verydemo.com/demo_c119_i960.html

转自
http://foyoto.iteye.com/blog/941654
1.当前时间
   Time.new 或Time.now
2.Time类的方法mktime(同义方法是local方法)来根据传入的参数生成时间对象,并且它使用的是当前的时间
Java代码
t1 = Time.mktime(2001)   => Mon Jan 01 00:00:00 +0800 2001 
t2 = Time.mktime(2001,3) => Thu Mar 01 00:00:00 +0800 2001 
t3 = Time.mktime(2001,3,15) => Thu Mar 15 00:00:00 +0800 2001 
t4 = Time.mktime(2001,3,15,21) => Thu Mar 15 21:00:00 +0800 2001 
t5 = Time.mktime(2001,3,15,21,30) => Thu Mar 15 21:30:00 +0800 2001 
t6 = Time.mktime(2001,3,15,21,30,15) => Thu Mar 15 21:30:15 +0800 2001 

   t1 = Time.mktime(2001)   => Mon Jan 01 00:00:00 +0800 2001
   t2 = Time.mktime(2001,3) => Thu Mar 01 00:00:00 +0800 2001
   t3 = Time.mktime(2001,3,15) => Thu Mar 15 00:00:00 +0800 2001
   t4 = Time.mktime(2001,3,15,21) => Thu Mar 15 21:00:00 +0800 2001
   t5 = Time.mktime(2001,3,15,21,30) => Thu Mar 15 21:30:00 +0800 2001
   t6 = Time.mktime(2001,3,15,21,30,15) => Thu Mar 15 21:30:15 +0800 2001
3.Time.gm(同义方法是Time.utc)方法基本上和上面的mktime用法相同,但它使用的是GMT或  UTC时区
Java代码
t8 = Time.gm(2001,3,15,21,30,15)  => Thu Mar 15 21:30:15 UTC 2001 
t9 = Time.utc(2001,3,15,21,30,15) => Thu Mar 15 21:30:15 UTC 2001 

   t8 = Time.gm(2001,3,15,21,30,15)  => Thu Mar 15 21:30:15 UTC 2001
   t9 = Time.utc(2001,3,15,21,30,15) => Thu Mar 15 21:30:15 UTC 2001
4.Time对象的to_a方法
生成的时间对象有一个to_a方法,可以把时间相关一信息转化成一个数组。
Java代码
t9.to_a => [15, 30, 21, 15, 3, 2001, 4, 74, false, "UTC"] 

t9.to_a => [15, 30, 21, 15, 3, 2001, 4, 74, false, "UTC"]
数组中存放的信息格式如下:
   seconds,
   minutes,
   hours,
   day,
   month,
   year,
   day of week (0..6),
   day of year (1..366),
   daylight saving (true or false),
   and time zone (as a string)
5.使用秒数来创建日期
   在内部,日期存储为一个整数,代表从1970年开始到当前的秒数,我们可以获取这么秒数或则利用这个秒数来创建日期:
Java代码
epoch = Time.at(0) # Find the epoch (Thu Jan 01 08:00:00 +0800 1970)  
newmil = Time.at(978307200) # Happy New Millennium! (Mon Jan 01 08:00:00 +0800 2001)  
now = Time.now # Sat Mar 05 16:25:00 +0800 2011 
sec = now.to_i # 1299313500 

    epoch = Time.at(0) # Find the epoch (Thu Jan 01 08:00:00 +0800 1970)
    newmil = Time.at(978307200) # Happy New Millennium! (Mon Jan 01 08:00:00 +0800 2001)
    now = Time.now # Sat Mar 05 16:25:00 +0800 2011
    sec = now.to_i # 1299313500


6.格式化时间
Java代码
   <PRE class=java name="code">time.strftime( string ) => string</PRE>  
<BR>   t = Time.now => Sat Mar 05 16:36:18 +0800 2011 
<BR>   t.strftime("%Y/%m/%d %H:%M") => "2011/03/05 16:36" 
<BR>   t.strftime("%Y/%m/%d") => "2011/03/05" 

   Java代码 time.strftime( string ) => string  time.strftime( string ) => string
   t = Time.now => Sat Mar 05 16:36:18 +0800 2011
   t.strftime("%Y/%m/%d %H:%M") => "2011/03/05 16:36"
   t.strftime("%Y/%m/%d") => "2011/03/05"
   所有strftime方法中可用的格式化符号:
      %a    星期的缩写,如Wed
      %A    星期的全称,如Wednesday
      %U    本星期在全年中所属的周数
      %W 
      %H    小时(24小时制)
      %M    分钟
      %S    秒
      %I    小时(12小时制)
      %p    PM 或 AM

      %b    月份的缩写,如 Jan
      %B    月份的全称,如 January
      %c    本地日期和时间,如 06/14/07 16:43:49
      %d    日期 (1..31)
      %j    本日在一年中所属的天 (1..366)
      %m    月份 (1..12)
      %w    星期的数字形式 (0..6)
      %x    本地日期,如 06/14/07
      %Y    本地时间,如 16:43:49
      %y    2位的年份表示,如07
      %Y    4位的年份表示,如2007
      %Z    时区名,如"中国标准时间"
      %%    字面符号%
7.Time对象的常用方法
Java代码
t = Time.now  
t= Time.now=> Sat Mar 05 16:57:35 +0800 2011 
t.year=> 2011#年  
t.month => 3#月  
t.day  => 5#日  
t.hour  => 16#小时  
t.min  => 57#分钟  
t.sec => 35#秒  
t.wday => 6#表示星期六,星期天为0 
t.yday => 64#一年的第几天 

    t = Time.now
    t= Time.now=> Sat Mar 05 16:57:35 +0800 2011
    t.year=> 2011#年
    t.month => 3#月
    t.day  => 5#日
    t.hour  => 16#小时
    t.min  => 57#分钟
    t.sec => 35#秒
    t.wday => 6#表示星期六,星期天为0
    t.yday => 64#一年的第几天
    还有to_a, to_i,to_s ....更多参考http://ruby-doc.org/core/
8.rails日期和时间的扩展
Java代码
now = Time.now => Sat Mar 05 17:09:55 +0800 2011 
now.to_date => Sat, 05 Mar 2011 
now.to_s => "Sat Mar 05 17:09:55 +0800 2011" 
now.to_s(:short) => "05 Mar 17:09" 
now.to_s(:long) => "March 05, 2011 17:09" 
now.to_s(:db) => "2011-03-05 17:09:55" 
now.to_s(:rfc822) => "Sat, 05 Mar 2011 17:09:55 +0800" 
now.at_beginning_of_day => Sat Mar 05 00:00:00 +0800 2011 
now.at_beginning_of_month => Tue Mar 01 00:00:00 +0800 2011 
now.at_beginning_of_week => Mon Feb 28 00:00:00 +0800 2011 
now.at_beginning_of_quarter => Sat Jan 01 00:00:00 +0800 2011 
now.at_beginning_of_year => Sat Jan 01 00:00:00 +0800 2011 
now.at_midnight => Sat Mar 05 00:00:00 +0800 2011 
now.change(:hour => 13) => Sat Mar 05 13:00:00 +0800 2011 
now.last_month => Sat Feb 05 17:09:55 +0800 2011 
now.last_year => Fri Mar 05 17:09:55 +0800 2010 
now.midnight => Sat Mar 05 00:00:00 +0800 2011 
now.monday => Mon Feb 28 00:00:00 +0800 2011 
now.months_ago(2) => Wed Jan 05 17:09:55 +0800 2011 
now.months_ago(1) => Sat Feb 05 17:09:55 +0800 2011 
now.months_ago(3) => Sun Dec 05 17:09:55 +0800 2010 
now.months_since(2) => Thu May 05 17:09:55 +0800 2011 
now.next_week => Mon Mar 07 00:00:00 +0800 2011 
now.next_year => Mon Mar 05 17:09:55 +0800 2012 
now.seconds_since_midnight => 61795.249 
now.since(7200) => Sat Mar 05 19:09:55 +0800 2011 
now.tomorrow => Sun Mar 06 17:09:55 +0800 2011 
now.years_ago(2) => Thu Mar 05 17:09:55 +0800 2009 
now.years_since(2) => Tue Mar 05 17:09:55 +0800 2013 
now.yesterday=> Fri Mar 04 17:09:55 +0800 2011 
now.advance(:days => 30) => Mon Apr 04 17:09:55 +0800 2011 
Time.days_in_month(2)  => 28 
Time.days_in_month(2,2000) => 29 

   now = Time.now => Sat Mar 05 17:09:55 +0800 2011
   now.to_date => Sat, 05 Mar 2011
   now.to_s => "Sat Mar 05 17:09:55 +0800 2011"
   now.to_s(:short) => "05 Mar 17:09"
   now.to_s(:long) => "March 05, 2011 17:09"
   now.to_s(:db) => "2011-03-05 17:09:55"
   now.to_s(:rfc822) => "Sat, 05 Mar 2011 17:09:55 +0800"
   now.at_beginning_of_day => Sat Mar 05 00:00:00 +0800 2011
   now.at_beginning_of_month => Tue Mar 01 00:00:00 +0800 2011
   now.at_beginning_of_week => Mon Feb 28 00:00:00 +0800 2011
   now.at_beginning_of_quarter => Sat Jan 01 00:00:00 +0800 2011
   now.at_beginning_of_year => Sat Jan 01 00:00:00 +0800 2011
   now.at_midnight => Sat Mar 05 00:00:00 +0800 2011
   now.change(:hour => 13) => Sat Mar 05 13:00:00 +0800 2011
   now.last_month => Sat Feb 05 17:09:55 +0800 2011
   now.last_year => Fri Mar 05 17:09:55 +0800 2010
   now.midnight => Sat Mar 05 00:00:00 +0800 2011
   now.monday => Mon Feb 28 00:00:00 +0800 2011
   now.months_ago(2) => Wed Jan 05 17:09:55 +0800 2011
   now.months_ago(1) => Sat Feb 05 17:09:55 +0800 2011
   now.months_ago(3) => Sun Dec 05 17:09:55 +0800 2010
   now.months_since(2) => Thu May 05 17:09:55 +0800 2011
   now.next_week => Mon Mar 07 00:00:00 +0800 2011
   now.next_year => Mon Mar 05 17:09:55 +0800 2012
   now.seconds_since_midnight => 61795.249
   now.since(7200) => Sat Mar 05 19:09:55 +0800 2011
   now.tomorrow => Sun Mar 06 17:09:55 +0800 2011
   now.years_ago(2) => Thu Mar 05 17:09:55 +0800 2009
   now.years_since(2) => Tue Mar 05 17:09:55 +0800 2013
   now.yesterday=> Fri Mar 04 17:09:55 +0800 2011
   now.advance(:days => 30) => Mon Apr 04 17:09:55 +0800 2011
   Time.days_in_month(2)  => 28
   Time.days_in_month(2,2000) => 29
  这些只是其中的一部分,如果想知道更多的函数可以参看rails api。你还可以参考下面这篇文章http://blog.zool.it/posts/rails-in-the-method-of-operation-of-time,他最后提到的by_star这个插件是用来做时间搜索的,我之前也用过,你可以去用下。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值