I want to generate weekly business dates between two dates excluding weekends and holidays. So I want a results
week 1
StartDate {2/18/2019 12:00:00 AM}
EndDate{2/25/2019 12:00:00 AM---wrong end date} ---> 2/27/2019 correct end date
week 2
StartDate {2/25/2019 12:00:00 AM}
EndDate{3/4/2019 12:00:00 AM--wrong end date}----->3/6/2019 -- correct end date
Below is my code. Any help will be appreciated
- class Program
- {
- public class WeekStartEnd
- {
- public DateTime firstDate;
- public DateTime SecondDate;
- }
- static void Main(string[] args)
- {
- //usage:
- DateTime StartDate = DateTime.Parse("02/12/2019");
- DateTime SeriesEndDate = DateTime.Parse("12/31/2025");
- DateTime firstMonday = Enumerable.Range(1, (SeriesEndDate - StartDate).Days +1)
- .SkipWhile(x => StartDate.AddDays(x).DayOfWeek != DayOfWeek.Monday)
- .Select(x => StartDate.AddDays(x))
- .First();
- TimeSpan ts = (TimeSpan)(SeriesEndDate - firstMonday);
- List
dates = new List (); - for (int i = 0; i < ts.Days; i += 7)
- {
- dates.Add(new WeekStartEnd() { firstDate = firstMonday.AddDays(i), SecondDate = firstMonday.AddDays(i + 7) });
- }
- Console.WriteLine(dates);
- }
- }
- }

Amit GuptaPosted Feb 22, 2019, 7:28 AM