Generally speaking, Time, Date and ParseDate can accomplish all of your
date-time calculation needs.
For your example, here’s one solution (there may be a better one):
years_to_subtract = 34
d1 = Date.new(2003,1,29)
d1.year → 2003
d2 = d1 << (years_to_subtract * 12)
d2.year → 1969
The << method subtracts months (hence the *12). I don’t know of a way to
subtract years.
On another note, I noticed something wonky with ParseDate in Windows
(1.6.7):
require “parsedate”
x = Time.now
a = ParseDate.parsedate(“#{x}”).slice(0…5)
In *nix, a[0] is 2003
In Windows, however, I get nil.
Any ideas anyone?
Regards,
Dan
···
-----Original Message-----
From: Chris Morris [mailto:chrismo@clabs.org]
Sent: Wednesday, January 29, 2003 10:25 AM
To: ruby-talk@ruby-lang.org
Subject: Date math lib?I’m in need of some date math routines – primarily a way to
add/subtract years from a date, which appears to be more than
just a no-brainer routine to do it Right™ (or maybe my
brain is in over-complication mode). Searching RAA and the
archives didn’t come up with much.Chris
http://clabs.org