Skip to content

Instantly share code, notes, and snippets.

@karoltheguy
Last active October 16, 2021 01:16
Show Gist options
  • Save karoltheguy/e0230486132d7e37ddb619f19c1a6855 to your computer and use it in GitHub Desktop.
Save karoltheguy/e0230486132d7e37ddb619f19c1a6855 to your computer and use it in GitHub Desktop.
Detect if date contains at least a full month (1st to last day of month)
using System;
public class Program
{
public static void Main(string[] args)
{
DateTime start = new DateTime(2020, 12, 15);
DateTime end = new DateTime(2021, 1, 20);
System.Diagnostics.Debug.WriteLine(IsFullMonth(start, end));
System.Diagnostics.Debug.Close();
}
private static bool IsFullMonth(DateTime start, DateTime end)
{
int start_firstday = 1;
int end_lastday = DateTime.DaysInMonth(end.Year, end.Month);
//2021 01 15 - 2021 03 15
if (end.Month - start.Month > 1)
return true;
//2021 03 01 - 2021 03 31
else if (end.Month - start.Month == 0 && end.Year == start.Year)
{
if (start.Day == start_firstday && end.Day == end_lastday)
return true;
return false;
}
//end.Month - start.Month >= 1
else
{
//2020 02 01 - 2021 03 31
//2020 12 01 - 2021 01 31
if (start.Day == start_firstday || end.Day == end_lastday)
return true;
//2020 07 15 - 2021 08 20
else if (start.Year < end.Year && start.Month != 12)
return true;
//2020 12 15 - 2021 08 22
//2020 12 02 - 2021 02 15
else if (start.Year < end.Year && start.Month - end.Month < 11)
return true;
return false;
}
}
}
@karoltheguy
Copy link
Author

Some unions calculate their members eligibility this way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment