🔨 Reduce code complexity in DateTime.Last

This commit is contained in:
Oliver Booth 2019-12-17 11:51:24 +00:00
parent d2208f2f6a
commit d246abd397
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
1 changed files with 5 additions and 4 deletions

View File

@ -66,10 +66,11 @@
/// <param name="dayOfWeek">The current day of week.</param>
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);
}
/// <summary>
@ -78,7 +79,7 @@
/// <param name="current">The current date.</param>
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);
}
/// <summary>