Created
May 21, 2021 21:48
-
-
Save mattjohnsonpint/ff9c702808117cb152b227977cadbfed to your computer and use it in GitHub Desktop.
DateOnly Between methods
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
public static int DaysBetween(DateOnly startDate, DateOnly endDate) | |
{ | |
return endDate.DayNumber - startDate.DayNumber; | |
} | |
public static (int Months, int Days) MonthsAndDaysBetween(DateOnly startDate, DateOnly endDate) | |
{ | |
int months = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month; | |
DateOnly testDate = startDate.AddMonths(months); | |
if (startDate <= endDate) | |
{ | |
if (testDate > endDate) months--; | |
} | |
else | |
{ | |
if (testDate < endDate) months++; | |
} | |
int days = DaysBetween(startDate.AddMonths(months), endDate); | |
return (months, days); | |
} | |
public static (int Years, int Months, int Days) YearsMonthsAndDaysBetween(DateOnly startDate, DateOnly endDate) | |
{ | |
(int months, int days) = MonthsAndDaysBetween(startDate, endDate); | |
int years = months / 12; | |
months %= 12; | |
return (years, months, days); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment