C method for getting the first and last day dates of the last month
- 2020-12-10 00:49:08
- OfStack
This article example shows how C# gets the first and last day dates of the last month. Share to everybody for everybody reference.
The specific implementation code is as follows:
int year = DateTime.Now.Year;// The current year
int mouth = DateTime.Now.Month;// The current month
int beforeYear = 0;
int beforeMouth = 0;
if (mouth <= 1)// If the current month is 1 Months, so the years have to be subtracted 1
{
beforeYear = year - 1;
beforeMouth =12;// Last month,
}
else
{
beforeYear = year;
beforeMouth = mouth - 1;// Last month,
}
string beforeMouthOneDay = beforeYear + " years " + beforeMouth + " month " + 1 + " day ";// Last month, the first 1 day
string beforeMouthLastDay = beforeYear + " years " + beforeMouth + " month " + DateTime.DaysInMonth(year, beforeMouth) + " day ";// Last month 1 day
On the last day of the month, you can also write:
string beforeMouthLastDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd"); // Get the last month 1 Day date
Hopefully this article has helped you with your C# programming.