From d246abd397af76b18930b046d39135a4ed94f2ab Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 17 Dec 2019 11:51:24 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Reduce=20code=20complexity=20in?= =?UTF-8?q?=20DateTime.Last?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/DateTimeExtensions.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/X10D/DateTimeExtensions.cs b/X10D/DateTimeExtensions.cs index 1bbcbe1..fb4874b 100644 --- a/X10D/DateTimeExtensions.cs +++ b/X10D/DateTimeExtensions.cs @@ -66,10 +66,11 @@ /// 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; - last = last.AddDays(Math.Abs(dayOfWeek - last.DayOfWeek) * -1); - return last; + int diff = -Math.Abs(dayOfWeek - lastDayOfWeek); + return last.AddDays(diff); } /// @@ -78,7 +79,7 @@ /// The current date. public static DateTime Midnight(this DateTime current) { - return new DateTime(current.Year, current.Month, current.Day); + return new DateTime(current.Year, current.Month, current.Day, 0, 0, 0); } ///