Topic: Finding exact date before specified months & weeks

Dear All,

Iam using ruby1.8.6 and rails 2.2.2

I have small issue when using Date functionality. My need is to find date when months or weeks is given as input like,

When 3 months is given as input i need to calculate the exact date before 3 months. Also when 2 weeks is given as input i need to calculate the exact date before 2 weeks.

I have tried (Date.today - 3.months) but its not working could any one plz help me out.

Thanks in advance

Regards,
Jose Martin

Re: Finding exact date before specified months & weeks

Depends what you mean by exact. The '2 weeks' one is easy, because that's always 14 days. '3 months' is more problematic - you can't just assume that 23 Jan is 3 months before 23 April, because months are different lengths, and February varies in size. Also the resulting date could be at a weekend, which might not be what you want...

Re: Finding exact date before specified months & weeks

Aren't the Rails duration helpers accurate enough?

3.months.ago

iirc they take into account the varying number of days in months.

Rob Anderton
TheWebFellas