Net.Bluewalk.DotNetUtils/Extensions/DateTimeExtensions.cs
/// <summary>
/// Get the closest date for target DayOfWeek
/// </summary>
/// <param name="date">The original date</param>
/// <param name="target">The target of DayOfWeek</param>
/// <returns>The closet date which is target day of week</returns>
Get the closest date for target DayOfWeek
Get the closest date for target DayOfWeek
public static DateTime GetClosestDate(this DateTime date, DayOfWeek target)
        {
            if (date.DayOfWeek == target)
            {
                return date;
            }
            if (date.DayOfWeek > target)
            {
                return date.AddDays(target - date.DayOfWeek).Date;
            }
            else
            {
                return date.AddDays(target - date.DayOfWeek - 7).Date;
            }
        }
public static DateTime GetClosestDate ( this DateTime date , DayOfWeek target ) { if ( date . DayOfWeek == target ) { return date ; } if ( date . DayOfWeek > target ) { return date . AddDays ( target - date . DayOfWeek ) . Date ; } else { return date . AddDays ( target - date . DayOfWeek - 7 ) . Date ; } }
Get the closest date for target DayOfWeek
Get the closest date for target DayOfWeek
The closet date which is target day of week
The closet date which is target day of week
DateTime
this
target
DayOfWeek
The target of DayOfWeek
The target of DayOfWeek
date
The original date
The original date