/// <summary> /// Get months between date /// </summary> /// <param name="one">Date one</param> /// <param name="two">Date two</param> /// <param name="includeLastDay">Include last day</param> /// <returns>Months</returns> public static int MonthsBetween(this DateTime one, DateTime two, bool includeLastDay) { if (!includeLastDay) return one.MonthsBetween(two); int days; if (two >= one) days = two.AddDays(1).DateValue() - one.DateValue(); else days = one.AddDays(1).DateValue() - two.DateValue(); return days / 31; }
/// <summary> /// Get months between date /// </summary> /// <param name="one">Date one</param> /// <param name="two">Date two</param> /// <returns>Months</returns> public static int MonthsBetween(this DateTime one, DateTime two) { var months = (two.DateValue() - one.DateValue()) / 31; return Math.Abs(months); }