C# - Get the first and last day of the given date

By , 8/26/2007
(1 ratings)
Gets the first day of the month from the given date.
Gets the last day of month from the given date.
public static class Month
{
    /// <summary>
    /// Gets the first day of the month.
    /// </summary>
    /// <param name="givenDate">The given date.</param>
    /// <returns>the first day of the month</returns>
    public static DateTime GetFirstDayOfMonth(DateTime givenDate)
    {
        return new DateTime(givenDate.Year, givenDate.Month, 1);
    }

    /// <summary>
    /// Gets the last day of month.
    /// </summary>
    /// <param name="givenDate">The given date.</param>
    /// <returns>the last day of the month</returns>
    public static DateTime GetTheLastDayOfMonth(DateTime givenDate)
    {
        return GetFirstDayOfMonth(givenDate).AddMonths(1).Subtract(new TimeSpan(1, 0, 0, 0, 0));
    }
}

Tagged with date, DateTime, month.

Comments

 

Log in, to comment!