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

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

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

In addition, my requirement is to get the cost from 52 weeks ago. I know that this corresponds to 1 year, but they were specific for about 52 weeks.

It would be the same for me:

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

but

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

I ask that .AddDays(-7 * 52) same as .AddWeeks(-52) because 7 days a week.

+7
source share
8 answers

As you noted in your question, unlike Years and Months, there is always exactly 7 days a week (at least in my calendar), so very little can be obtained thanks to the AddWeeks method, when all you need to do.AddDays (weeks * 7). Although you should question the logic when they have AddMinutes and AddHours! Curse them and their inconsistencies!

You can always create an extension method for .AddWeeks if this really bothers you:

 public static class DateTimeExtensions { public static DateTime AddWeeks(this DateTime dateTime, int numberOfWeeks) { return dateTime.AddDays(numberOfWeeks * 7); } } 

And, as others have pointed out, the year is not 52 weeks.

+15
source

The abstract Calendar class implements the method that you need , you can use your language calendar or create an object class that implements it:

 DateTime dt = CultureInfo.InvariantCulture.Calendar.AddWeeks(datetime, weeks); GregorianCalendar gc = new GregorianCalendar(); DateTime dt = gc.AddWeeks(datetime, weeks); 
+4
source

Ultimatey I expect AddWeeks to be absent to avoid a myriad of methods. Perhaps add an extension method:

 public static DateTime AddWeeks(this DateTime from, int count) { return from.AddDays(7 * count); } 
+2
source

yearOldPrice = _priceService.GetPriceForDate(price.Date.AddDays(-7 * 52); is what you want. Note that adding a year and adding 52 weeks is different.

If you really want, you could make an extension method :

 public static class DateTimeExtensions { public static DateTime AddWeeks(this DateTime DT, int Weeks) { return DT.AddDays(Weeks * 7); } } 
+1
source

It would be a little different. Subtracting 52 weeks subtracts 364 days, while the year is 365 (366 for leap years).

There is probably no AddWeeks() , because AddDays(-7 * numWeeks) is easy enough to subtract weeks.

+1
source

This is the same as a week = 7 days (.AddDays (-7 * 52) ==. AddWeeks (-52))

but 52 weeek is not a year (.AddDays (-7 * 52)! = .AddYears (-1))

0
source

If they were specific for approximately 52 weeks, I would use -7 * 52, as always 7 days a week. Using .AddYear will allow for leap years when an extra day exists.

0
source

I think this is due to the fact that there are several definitions of week numbers there.

The definition of determining the week number in ISO ( https://en.wikipedia.org/wiki/ISO_week_date ) is different from the North American definition of week number and other cultures.

Why the above explanation matters to our context (why not have "AddWeeks ()")

The beginning of the 1st week or the end of the 52/53 week varies in different formats / cultures, therefore when adding / subtracting, if we cross this border, i.e. the beginning of the year or the end of the year, then it is necessary to obtain additional information in order to determine the exact date for the week number (regardless of whether they correspond to the ISO or forms of local culture, whether Monday or Sunday, etc.), which makes this function complicated. I think that’s why they left it to us to find out the week number.

If you literally want to add a few weeks, and if you don't like the definition of the week, then yes, the Steve Morgan solution proposed above is smart enough.

0
source

All Articles