Skip to content

Instantly share code, notes, and snippets.

@SamKr
Created January 8, 2019 10:37
Show Gist options
  • Save SamKr/1c5498e4d44b6ea7991b885eca3aca88 to your computer and use it in GitHub Desktop.
Save SamKr/1c5498e4d44b6ea7991b885eca3aca88 to your computer and use it in GitHub Desktop.
Get first monday of given year/month
internal static DateTime GetFirstMonday(int month, int year)
{
var dt = new DateTime(year, month, 1, 12, 00, 00);
for (var i = 0; i < 7; i++)
{
if (dt.DayOfWeek == DayOfWeek.Monday)
{
return dt;
}
dt = dt.AddDays(1);
}
Console.WriteLine($"Unable to find 1st monday for year '{year}' and month '{month}'!");
return DateTime.MinValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment