/// <summary>Returns first days in range.</summary> /// <param name="range">The date range.</param> /// <returns>First days of months in range</returns> public static IEnumerable <DateTime> DaysBetween(this Range <DateTime> range) { range = range.MakeInclusive(); if (range.IsEmpty) { yield break; } var startDate = range.FromValue; var endDate = range.ToValue; while (startDate <= endDate) { yield return(startDate); startDate = startDate.NextDay(); } }
/// <summary>Returns first days of years in range.</summary> /// <param name="range">The date range.</param> /// <returns>First days of years in range</returns> public static IEnumerable <DateTime> YearsBetween(this Range <DateTime> range) { range = range.MakeInclusive(); if (range.IsEmpty) { yield break; } var startDate = range.FromValue.FirstDayOfYear(); var endDate = range.ToValue; // if range.FromValue is not first date of year, the years is skipped. if (startDate < range.FromValue) { startDate = startDate.NextYear(); } while (startDate <= endDate) { yield return(startDate); startDate = startDate.NextYear(); } }
/// <summary>Returns delta between two dates measured in months.</summary> /// <param name="range">The date range.</param> /// <returns>Delta between two dates measured in months.</returns> public static int DifferenceInMonths(this Range <DateTime> range) { range = range.MakeInclusive(); return(DifferenceInMonths(range.FromValue, range.ToValue)); }
/// <summary>Replaces exclusive boundaries with inclusive ones.</summary> /// <param name="range">The date range.</param> /// <returns>A range with inclusive boundaries.</returns> public static Range <DateTime> MakeInclusive(this Range <DateTime> range) => range.MakeInclusive(d => d.NextDay(), d => d.PrevDay());