Have a Snippet?

Keep, track and share your code snippets with your friends



C#: Get the first and last day of the given date Share on Vkontakte

Gets the first day of the month from the given date.
Gets the last day of month from the given date.

public static class MonthExtensions
{
    /// 
    /// Gets the first day of the month.
    /// 
    /// The given date.
    /// the first day of the month
    public static DateTime GetFirstDayOfMonth(DateTime givenDate)
    {
        return new DateTime(givenDate.Year, givenDate.Month, 1);
    }

    /// 
    /// Gets the last day of month.
    /// 
    /// The given date.
    /// the last day of the month
    public static DateTime GetTheLastDayOfMonth(DateTime givenDate)
    {
        return GetFirstDayOfMonth(givenDate).AddMonths(1).Subtract(new TimeSpan(1, 0, 0, 0, 0));
    }
}


Tag: C#, .NET, DateTime

0 Comments