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#.
Asked
Active
Viewed 4.8k times
35
-
Do you mean weeks as in Calender ? – V4Vendetta May 05 '11 at 06: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. – V4Vendetta May 05 '11 at 06:37
-
Kindly check, http://stackoverflow.com/questions/13970641/count-the-number-of-inclusive-dates-covered-by-a-date-period – Devesh Aug 30 '16 at 08:34
3 Answers
70
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.

Oded
- 489,969
- 99
- 883
- 1,009
-
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. – McAzzaMan May 05 '11 at 06: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. – DAG Nov 24 '14 at 14:22
-
2
-
@Oded, there are two ways to count weeks. See 'V4Vendetta' remark in the OP comments. The question is ambiguous. – DAG Nov 24 '14 at 14:46
-
2I get "can't convert double to int" with this; I'm trying: int numberOfWeeks = Convert.ToInt32(Math.Round((_delPerfEndDate - _delPerfBeginDate).TotalDays / 7)); – B. Clay Shannon-B. Crow Raven Mar 14 '16 at 23:21
-
@B.ClayShannon - Why are you doing a `Round` there (never mind wrapping that with a `Convert.ToInt32`)? `int numberOfWeeks = (int)(_delPerfEndDate - _delPerfBeginDate).TotalDays / 7);` a cast should be enough. – Oded Mar 15 '16 at 00:13
-
With (int) only, a date range from 1/31/2016 to 2/27/2016 returns 3. The round makes it 4, which is what we need it to be (Sunday 1/31 to Saturday 2/27 is four weeks as far as we are concerned). – B. Clay Shannon-B. Crow Raven Mar 15 '16 at 22:19
-
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 a `checked` context), but that would be a different exception. – Oded Mar 15 '16 at 23:07
7
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.

V4Vendetta
- 37,194
- 9
- 78
- 82
-
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. – DAG Nov 24 '14 at 14:25
-
@DAG can you please elaborate on what the shortcoming is and how it can be fixed? – Muhammad Mamoor Khan Jun 24 '19 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. – DAG Jun 24 '19 at 19:14
4
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!

jpaugh
- 6,634
- 4
- 38
- 90

Algorhythm
- 570
- 4
- 17