I’m doing some work where a user can select data based on choice like “This week” and “Last month”. I wrote a bunch of extension methods that are pretty generic and may be useful for others.
Here is is:
public static class DateTimeExtensions
{
/// <summary>
/// Return the date that is the start of the week relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfWeek(this DateTime date)
{
DayOfWeek day = date.DayOfWeek;
int days = day – CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
DateTime start = date.AddDays(-days);
return start.Date;
}
/// <summary>
/// Return the date that is the start of the week relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfLastWeek(this DateTime date)
{
return date.GetStartOfWeek().AddDays(-7);
}
/// <summary>
/// Return the date that is the end of the week relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfWeek(this DateTime date)
{
return date.GetStartOfWeek().AddDays(6);
}
/// <summary>
/// Return the date that is the end of the week relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfLastWeek(this DateTime date)
{
return date.GetEndOfWeek().AddDays(-7);
}
/// <summary>
/// Return the date that is the start of the month relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
/// <summary>
/// Return the date that is the start of previous month relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfLastMonth(this DateTime date)
{
return date.GetStartOfMonth().AddMonths(-1);
}
/// <summary>
/// Return the date that is the end of the month relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, date.GetDaysInMonth(), 23, 59, 59, 999);
}
/// <summary>
/// Return the date that is the start of previous month relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfLastMonth(this DateTime date)
{
return date.GetStartOfLastMonth().GetEndOfMonth();
}
/// <summary>
/// Returns the number of days in the month of the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static int GetDaysInMonth(this DateTime date)
{
return DateTime.DaysInMonth(date.Year, date.Month);
}
/// <summary>
/// Return the first day of the year relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfYear(this DateTime date)
{
return new DateTime(date.Year, 1, 1);
}
/// <summary>
/// Return the first day of the last year relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetStartOfLastYear(this DateTime date)
{
return new DateTime(date.Year - 1, 1, 1);
}
/// <summary>
/// Return the last day of the year relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfYear(this DateTime date)
{
return new DateTime(date.Year, 12, 31, 23, 59, 59, 999);
}
/// <summary>
/// Return the last day of the last year relative to the specified date.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetEndOfLastYear(this DateTime date)
{
return new DateTime(date.Year - 1, 12, 31, 23, 59, 59, 999);
}
}