Created
November 14, 2020 18:02
-
-
Save fabio-stein/f307a0c3d8bf183fb122d1445cd2387e to your computer and use it in GitHub Desktop.
Get Nth Business Day C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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