2gua 发表于 2010-12-10 16:43

[技巧]日期操作

你可以直接用Date对象加/减天数或月数:

    +(n):加n天
    -(n):减n天
    >>(n):加n月
    <<(n):减n月

一些例子:

$ irb
>> require 'date'
=> true

>> date = Date.today
>> date.to_s
=> "2010-12-10"

>> tomorrow = date + 1
>> tomorrow.to_s
=> "2010-12-11"

>> nextmonth = date >> 1
>> nextmonth.to_s
=> "2011-01-10"

查看Ruby文档了解相关详细信息。

zhlong8 发表于 2010-12-10 17:20

感觉这么用操作符挺诡异的{:3_196:}

哪个是加一年

2gua 发表于 2010-12-10 20:39

回复 2# zhlong8


    这个容易,月份+12。:em03:
页: [1]
查看完整版本: [技巧]日期操作