Click here to Skip to main content
15,071,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
My requirement is to find the previous months's first and last day using current date.
I tried kinda hard but unfortunately could not crack this. I need this urgently. Please help
Posted

1 solution

C#
DateTime date = DateTime.Now.AddMonths(-1);
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
   
v2
Comments
   
Looks correct, a 5.
(This is a well-known technique, could be found in several places though.)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900