Date math makes me crazy

I just don’t understand this and leap years make this problem obvious to
me…

test = DateTime.now
=> #<DateTime: 53017684949923739/21600000000,-7/24,2299161>

test.strftime("%Y-%m-%d")
=> “2008-02-25”

test = DateTime.now + 365
=> #<DateTime: 70700758603797619/28800000000,-7/24,2299161>

test.strftime("%Y-%m-%d")
=> “2009-02-24”

seems reasonable, off a day because of leap year…

test = DateTime.now + 1.year
=> #<DateTime: 1469323689922559749/43200000000,-7/24,2299161>

test.strftime("%Y-%m-%d")
=> “88409-12-04”

how do I add a simple year?

Craig

Craig W. wrote:

how do I add a simple year?

Craig

I did DateTime.now.year will give 2008. DateTime.now.year + 1 give 2009

On 25 Feb 2008, at 23:52, Craig W. wrote:

=> #<DateTime: 70700758603797619/28800000000,-7/24,2299161>
how do I add a simple year?
Time.now.next_year

Don’t use DateTime.

Best regards

Peter De Berdt

On Feb 25, 2008, at 4:52 PM, Craig W. wrote:

test = DateTime.now + 365

how do I add a simple year?

Craig

Hi Craig,

You might find this interesting:

Peace,
Phillip

On Tue, 2008-02-26 at 00:03 +0100, Peter De Berdt wrote:

how do I add a simple year?

Time.now.next_year

Don’t use DateTime.


gotcha…

this is really painful,
test = ((DateTime.now.year + 1).to_s + “-” + DateTime.now.month.to_s +
“-” + DateTime.now.day.to_s).to_date

but I do need dates so I have to convert back to a date but thankfully,
that isn’t so hard to do.

Thanks

Craig

On Feb 25, 2008, at 6:03 PM, Peter De Berdt wrote:

test = DateTime.now + 365

how do I add a simple year?

Time.now.next_year

Don’t use DateTime.

Best regards

Peter De Berdt

Well, since DateTime < Date, you can

test >>= 12

to advance test by 12 months. (and if you try in 4 days (on
2008-02-29), the result will be 2009-02-28 :wink:

But you probably want either a Date or a Time anyway.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]