Functions to get First Date and Last Date of Previous Month and Year.

private DateTime LastDateOfPreviousMonth()
{
    return DateAndTime.DateAdd("d", -1, DateTime.Now.Month.ToString() + "/01/" + DateTime.Now.Year.ToString());
}

Note:
In C#, you will need to add the namespace as 'using Microsoft.VisualBasic' in the source code unit. Also Add Reference to 'Microsoft.VisualBasic' dll.

private DateTime FirstDateOfPreviousMonth()
{
    int Yr, Mnth;
    DateTime prev = DateTime.Now.AddMonths(-1);
    Yr = prev.Year;
    Mnth = prev.Month;

    return Convert.ToDateTime(Mnth.ToString() + "/01/" + Yr.ToString());
}

0 comments