Why is there no DateTime.AddWeeks(), and how can I get a DateTime object for 52 weeks ago?

Viewed 23365

The System.DateTime object has methods to AddYears(), AddMonths(), AddDays(), AddSeconds(), etc.

I've noticed that there is no AddWeeks(). Why is this?

Also, my requirement is to get a price value from 52 weeks ago. I know this equates to 1 year, but they were specific about 52 weeks.

Would it be the same for me to do:

yearOldPrice = _priceService.GetPriceForDate(price.Date.AddYears(-1));

as

yearOldPrice = _priceService.GetPriceForDate(price.Date.AddDays(-7 * 52));

I ask on the presumption that .AddDays(-7 * 52) is the same as .AddWeeks(-52), 'cause there's 7 days in a week.

9 Answers

As @Craig said: In Powershell you could solve it like this:

[int]$WeeksAgo = 52

(Get-Date).AddDays(-7 * $WeeksAgo)
Related