diff --git a/X10D/src/DateTimeExtensions.cs b/X10D/src/DateTimeExtensions.cs index 1159aea..4577b8b 100644 --- a/X10D/src/DateTimeExtensions.cs +++ b/X10D/src/DateTimeExtensions.cs @@ -8,20 +8,32 @@ public static class DateTimeExtensions { /// - /// Calculates someone's age based on a date of birth. + /// Returns a rounded integer of the number of years since a given date as of today. /// - /// The date of birth. - public static int Age(this DateTime dateOfBirth) + /// The date from which to start. + /// Returns the number of years since as of today. + public static int Age(this DateTime date) { - return (int) (((DateTime.Today - TimeSpan.FromDays(1) - dateOfBirth.Date).TotalDays + 1) / 365.2425); + return date.Age(DateTime.Today); } /// - /// Gets a DateTime representing the first specified day in the current month + /// Returns a rounded integer of the number of years since a given date as of another given date. + /// + /// The date from which to start. + /// The date at which to stop counting. + /// Returns the number of years since as of . + public static int Age(this DateTime date, DateTime asOf) + { + return (int)(((asOf.Date - TimeSpan.FromDays(1) - date.Date).TotalDays + 1) / 365.2425); + } + + /// + /// Gets a DateTime representing the first occurence of a specified day in the current month. /// /// The current day /// The current day of week - /// + /// Returns a date which representing the first occurence of . public static DateTime First(this DateTime current, DayOfWeek dayOfWeek) { DateTime first = current.FirstDayOfMonth(); @@ -49,10 +61,8 @@ /// The current date. public static DateTime LastDayOfMonth(this DateTime current) { - int daysInMonth = DateTime.DaysInMonth(current.Year, current.Month); - - DateTime last = current.FirstDayOfMonth().AddDays(daysInMonth - 1); - return last; + var daysInMonth = DateTime.DaysInMonth(current.Year, current.Month); + return new DateTime(current.Year, current.Month, daysInMonth); } /// @@ -62,24 +72,13 @@ /// The current day of week. public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek) { - DateTime last = current.LastDayOfMonth(); + DateTime last = current.LastDayOfMonth(); DayOfWeek lastDayOfWeek = last.DayOfWeek; - int diff = -Math.Abs(dayOfWeek - lastDayOfWeek); - return last.AddDays(diff); - } + var diff = dayOfWeek - lastDayOfWeek; + var offset = diff > 0 ? diff - 7 : diff; - /// - /// Gets a representing midnight on the current date. - /// - /// The current date. - [Obsolete( - "This method has been deprecated in favor of Humanizer's fluent DateTime API. " + - "Please consider downloading the Humanizer package for more stable implementations of this method." - )] - public static DateTime Midnight(this DateTime current) - { - return new DateTime(current.Year, current.Month, current.Day, 0, 0, 0); + return last.AddDays(offset); } /// @@ -90,28 +89,14 @@ /// The day of week for the next date to get. public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek) { - int offsetDays = dayOfWeek - current.DayOfWeek; + var offsetDays = dayOfWeek - current.DayOfWeek; if (offsetDays <= 0) { offsetDays += 7; } - DateTime result = current.AddDays(offsetDays); - return result; - } - - /// - /// Gets a representing noon on the current date. - /// - /// The current date. - [Obsolete( - "This method has been deprecated in favor of Humanizer's fluent DateTime API. " + - "Please consider downloading the Humanizer package for more stable implementations of this method." - )] - public static DateTime Noon(this DateTime current) - { - return new DateTime(current.Year, current.Month, current.Day, 12, 0, 0); + return current.AddDays(offsetDays); } ///