November 5, 2009

Find the Last Day of the Previous Month using C# and VB.NET




A couple of days ago, I had written a post to Find the First and Last Day of the Current Quarter using C#/VB.NET. A user commented asking how to find the last day of the previous month. Here’s how it can be done:

C#

DateTime dt = DateTime.Now;
DateTime lstDay = new DateTime(dt.Year, dt.Month, 1);
lstDay = lstDay.AddDays(-1);
Console.WriteLine("Month: {0}, LastDate: {1}",lstDay.Month, lstDay.Day);
Console.Read();

VB.NET

Dim dt As DateTime = DateTime.Now
Dim lstDay As New DateTime(dt.Year, dt.Month, 1)
lstDay = lstDay.AddDays(-1)
Console.WriteLine("Month: {0}, LastDate: {1}",lstDay.Month, lstDay.Day)
Console.Read()

Output

image

The current month is November, so the last day of the previous month (October) is 31.



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Find the Last Day of the Previous Month using C# and VB.NET"
 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions