Answer the question
In order to leave comments, you need to log in
How to calculate weeks between 2 dates in C#?
Hello.
There are 2 DateTimes. Let's call them the beginning and the end.
Let's say our beginning is 01/15/2016, and the end is 02/05/2016.
We need a function that would return a Dictionary of the intervals of the beginning and end of weeks.
With these parameters, it would return 4 objects.
But, there is a condition. If there is a gap between the beginning and the end of the month, then you need to finish it in one interval, and start on the first day of the month in the other, as shown in the picture.
Should return 5 objects, where the third has a start/end of 30/31, and the fourth has 1/5.
Answer the question
In order to leave comments, you need to log in
1. Calculate the week number for the first and second dates.
2. For all weeks between the first and second, we issue the first day of the week and the last (or all days of the week, depending on what is needed).
3. For each object, we check whether there was a change of month, if so, we divide the object by two.
This is if in general.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question