Skip to content

Instantly share code, notes, and snippets.

@fabio-stein
Created November 14, 2020 18:02
Show Gist options
  • Save fabio-stein/f307a0c3d8bf183fb122d1445cd2387e to your computer and use it in GitHub Desktop.
Save fabio-stein/f307a0c3d8bf183fb122d1445cd2387e to your computer and use it in GitHub Desktop.
Get Nth Business Day C#
using System;
namespace TestApp
{
public class DateUtil
{
public static DateTime Get5thBusinessDay(DateTime sourceMonth)
{
return GetNthBusinessDay(5, sourceMonth);
}
public static DateTime GetNthBusinessDay(int nth, DateTime sourceDate, bool isSaturdayBusinessDay = false)
{
if (nth <= 0)
throw new Exception("Invalid 'nth' input");
var date = new DateTime(sourceDate.Year, sourceDate.Month, 1);
int businessDays = 0;
while (true)
{
if (IsHoliday(date)
|| (date.DayOfWeek == DayOfWeek.Saturday && !isSaturdayBusinessDay)
|| (date.DayOfWeek == DayOfWeek.Sunday))
{
date = date.AddDays(1);
continue;
}
if (++businessDays == nth)
return date;
date = date.AddDays(1);
}
}
public static bool IsHoliday(DateTime date)
{
//TODO Check JSON Holidays
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment