Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last day of the month in .NET

Tags:

.net

Normally I use the below code, but is there a better way?

lastOfMonth = new DateTime(Now.Year, Now.Month, 1).AddMonths(1).AddDays(-1) 
like image 582
Jonathan Allen Avatar asked Mar 09 '10 22:03

Jonathan Allen


People also ask

How do I get the last day of the month in C#?

AddMonths(1). AddDays(-3);". (-3) is the day amount so 0 of next month is basicly the last day of current month.

How do I get the last day of the current month in VB net?

try yo use DaysInMonth Function which returns the number of days in the required month. This will return (31), so you can get the date format in addition to this number to get last day of this month.

How do I get the first day of the month in VB net?

Getting StartedThe solution contains a single Windows Forms project called FirstLastDayVB which was written in VB.NET; the application contains a single Windows form (Form1. vb); this form contains all of the code necessary to calculate and display the first and last days of the month.


1 Answers

I use

DateTime now = DateTime.Today; var lastDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month)); 
like image 59
Mike Two Avatar answered Sep 20 '22 13:09

Mike Two