D
D
Dmitry Gavrilenko2016-02-05 17:15:42
.NET
Dmitry Gavrilenko, 2016-02-05 17:15:42

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.
e4455b82b0b14c2a8d6fb2df0f853038.PNG
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.
793c2f94ff9d4c9ca008a70388c5b30b.PNG
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

3 answer(s)
D
Dmitry Gavrilenko, 2016-02-08
@Maddox

For those who are interested here is the project .

A
Aleksej, 2016-02-05
@Shwed_Berlin

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.

P
Peter, 2016-02-05
@petermzg

And how did one week become two for you from 30.05 to 05.06?
It is considered simple. It turns out the number of days between dates and divides by 7.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question