7developers blog

Useful articles, videos and code examples from professional developers

C# DateTime Extensions

How do I extend a class with c# extension methods?
public static class DateTimeExtensions
{
    /// 
    /// Return the date that is the start of the week relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfWeek(this DateTime date)
    {
        DayOfWeek day = date.DayOfWeek;
        int days = day - DayOfWeek.Monday;
        DateTime start = DateTime.Now.AddDays(-days);
        return start.Date;
    }

    /// 
    /// Return the date that is the start of the week relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfLastWeek(this DateTime date)
    {
        return date.GetStartOfWeek().AddDays(-7);
    }

    /// 
    /// Return the date that is the end of the week relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfWeek(this DateTime date)
    {
        return date.GetStartOfWeek().AddDays(6);
    }

    /// 
    /// Return the date that is the end of the week relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfLastWeek(this DateTime date)
    {
        return date.GetEndOfWeek().AddDays(-7);
    }

    /// 
    /// Return the date that is the start of the month relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfMonth(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, 1);
    }

    /// 
    /// Return the date that is the start of previous month relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfLastMonth(this DateTime date)
    {
        return date.GetStartOfMonth().AddMonths(-1);
    }

    /// 
    /// Return the date that is the end of the month relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfMonth(this DateTime date)
    {
        return new DateTime(date.Year, date.Month, date.GetDaysInMonth(), 23, 59, 59, 999);
    }

    /// 
    /// Return the date that is the start of previous month relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfLastMonth(this DateTime date)
    {
        return date.GetStartOfLastMonth().GetEndOfMonth();
    }

    /// 
    /// Returns the number of days in the month of the specified date.
    /// 
    /// 
    /// 
    public static int GetDaysInMonth(this DateTime date)
    {
        return DateTime.DaysInMonth(date.Year, date.Month);
    }

    /// 
    /// Return the first day of the year relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfYear(this DateTime date)
    {
        return new DateTime(date.Year, 1, 1);
    }

    /// 
    /// Return the first day of the last year relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetStartOfLastYear(this DateTime date)
    {
        return new DateTime(date.Year - 1, 1, 1);
    }

    /// 
    /// Return the last day of the year relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfYear(this DateTime date)
    {
        return new DateTime(date.Year, 12, 31, 23, 59, 59, 999);
    }

    /// 
    /// Return the last day of the last year relative to the specified date.
    /// 
    /// 
    /// 
    public static DateTime GetEndOfLastYear(this DateTime date)
    {
        return new DateTime(date.Year - 1, 12, 31, 23, 59, 59, 999);
    }

}
Loading