0

I'm working on a project which requires me to get the next 7th business day of the month.

I tried the solution below but I'm not satisfied with the result, furthermore, it doesn't take into account the holidays (in France). I guess I could use an array with most of the holidays and filter through it.

Could you give me hints or a sample of code to correct me? Best regards.

DateTime seventhBusinessDayNextMonth = new DateTime(DateTime.Today.AddMonths(1).Year, DateTime.Today.AddMonths(1).Month, 7);
DateTime seventhBusinessDayCurrentMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 7);

if (DateTime.Today.Day == 7 && DateTime.Today.DayOfWeek != DayOfWeek.Saturday && DateTime.Today.DayOfWeek != DayOfWeek.Sunday)
{
    this.Data.nextSeventhBusinessDay = DateTime.Now.ToString("dd/MM/yyyy");
}
else if (DateTime.Today.Day <= 7 && seventhBusinessDayCurrentMonth.DayOfWeek == DayOfWeek.Saturday)
{
    this.Data.nextSeventhBusinessDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 9).ToString("dd/MM/yyyy");
}
else if (DateTime.Today.Day <= 7 && DateTime.Today.DayOfWeek == DayOfWeek.Sunday)
{
    this.Data.nextSeventhBusinessDay = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 8).ToString("dd/MM/yyyy");
}
else if (DateTime.Today.Day > 7 && seventhBusinessDayNextMonth.DayOfWeek == DayOfWeek.Saturday)
{
    this.Data.nextSeventhBusinessDay = new DateTime(DateTime.Today.AddMonths(1).Year, DateTime.Today.AddMonths(1).Month, 9).ToString("dd/MM/yyyy");
}
else if (DateTime.Today.Day > 7 && seventhBusinessDayNextMonth.DayOfWeek == DayOfWeek.Sunday)
{
    this.Data.nextSeventhBusinessDay = new DateTime(DateTime.Today.AddMonths(1).Year, DateTime.Today.AddMonths(1).Month, 8).ToString("dd/MM/yyyy");
}
else
{
    this.Data.nextSeventhBusinessDay = seventhBusinessDayNextMonth.ToString("dd/MM/yyyy");
}
Milad Dastan Zand
  • 1,086
  • 1
  • 10
  • 20
Lowick
  • 1
  • If you're working with any kind of database it's not uncommon to have a Calendar table there. And indeed not uncommon to have WorkingDay be one of the columns in that table. Because then you work out the rules that make sense for *your* business, your country, etc, and populate it with 20 years worth at a time. – Damien_The_Unbeliever Oct 26 '21 at 14:51

0 Answers0