# 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 White wrote:

how do I add a simple year?

Craig

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

Time.now.next_year

Donâ€™t use DateTime.

Best regards

Peter De Berdt

> 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? >

Time.now.next_year

Don't use DateTime.

Hi Craig,

You might find this interesting:

Peace, Phillip

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

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

-Rob

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com