Exactly as it says on the tin, I just need the most efficient way of counting weeks (i.e. 7-day spans, not calendar weeks) between two dates in C#.
-
Do you mean weeks as in Calender ?– V4VendettaCommented May 5, 2011 at 6:11
-
2What i meant is two approach whereby any 7 day period could be a week and the other where it follows the calender which has got weeks at specific date ranges. Since you are dealing in dates its more relevant to check with calender weeks.– V4VendettaCommented May 5, 2011 at 6:37
-
Kindly check, stackoverflow.com/questions/13970641/…– DeveshCommented Aug 30, 2016 at 8:34
3 Answers
Get the number of days and divide by 7.
int weeks = (date1 - date2).TotalDays / 7;
You may well have a remainder of up to 6 days that will not be included in the number of weeks.
-
1Thanks. You would not believe how many garbage answers there are for this online, and i'm having a bit of a brain-fart day and couldn't come up with anything. Commented May 5, 2011 at 6:12
-
1This answer does not work when you want to know the other 'type' of weeks measurement - whereby calendar weeks is desired, not any arbitrary span. So for example say you have a frequency criteria whereby something should be done "once a calendar week". If it was done on a Saturday, then on Sunday you want to see the result of "1" because now it's a new calendar week. To get a zero you would have to be comparing two dates in the same calendar week. This answer will give "0" and continue to give zero until the next Saturday. See 'V4Vendetta' remark in the OP comments.– DAGCommented Nov 24, 2014 at 14:22
-
3@DAG - did you read the question? That is not what it asked for.– OdedCommented Nov 24, 2014 at 14:28
-
2I get "can't convert double to int" with this; I'm trying: int numberOfWeeks = Convert.ToInt32(Math.Round((_delPerfEndDate - _delPerfBeginDate).TotalDays / 7)); Commented Mar 14, 2016 at 23:21
-
1@B.ClayShannon - I see. Other ways to do that, but I can't reproduce the conversion error. Can only figure a problem if the result would overflow an
Int32
(and the code is in achecked
context), but that would be a different exception.– OdedCommented Mar 15, 2016 at 23:07
I assume you want to get this on the basis of the Calender. For this you need System.Globalization
DateTime date1 = DateTime.Now;
DateTimeFormatInfo dinfo = DateTimeFormatInfo.CurrentInfo;
dinfo.Calendar.GetWeekOfYear(date1, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday)
Based on your need you have to set the Calender week rule and the first day of the week.
This gives you a week number for the calender. you can get the same for your other date, the difference is your weeks count
Hope this helps you.
-
This fails under the very common scenario of two dates spanning different years. Not all years are 52 weeks, so your answer doesn't have an immediately obvious fix.– DAGCommented Nov 24, 2014 at 14:25
-
@DAG can you please elaborate on what the shortcoming is and how it can be fixed? Commented Jun 24, 2019 at 16:24
-
If the dates are on different years, you need more logic and math to get your answer, in addition to calculating (finding) the total weeks of the year for the date that is in the older year. The fixes should not be pursued, instead I suggest using the 'best answer' for this question.– DAGCommented Jun 24, 2019 at 19:14
Try this to get the number of days:
TimeSpan ts = date1.Subtract(date2);
int dateDiff = ts.Days();
Then, like @Oded said, divide by 7
int totalWeeks = (int) dateDiff / 7;
Cheers!