Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I need list of dates that between two dates but i have to remove weekends. How can i do? Thanks.

For example i want select between 02.02.2021 and 17.02.2021 (Actually between more than 10 years).

Remove weekends
06.02.2021
07.02.2021
13.02.2021
14.02.2021

This list must return as
02.02.2021
03.02.2021
04.02.2021
05.02.2021
08.02.2021
09.02.2021
10.02.2021
11.02.2021
12.02.2021
15.02.2021
16.02.2021
17.02.2021


What I have tried:

I have no idea. (This area must have more than 30 characters.)
Posted
Updated 28-Feb-21 23:28pm

1 solution

Try:
C#
int numberOfDaysTotal = 30;
DateTime start = DateTime.Today;
var dates = Enumerable.Range(0, numberOfDaysTotal)
            .Select(offset => start.AddDays(offset))
            .Where(date => date.DayOfWeek != DayOfWeek.Saturday 
                        && date.DayOfWeek != DayOfWeek.Sunday);
 
Share this answer
 
Comments
Maciej Los 1-Mar-21 6:49am    
5ed!
gacar 1-Mar-21 7:50am    
Perfect!! Many thanks.
OriginalGriff 1-Mar-21 7:57am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900