Click here to Skip to main content
15,898,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please i will like to know how to get the last day of the month.

i am writing a code which needs me to provide an expiring date as a parameter, now the expiring date as to be the last date of the month.

i can get today date by doing this
C#
DateTime now = DateTime.Today;


but how do i get the last date of the month with this.

thanks

What I have tried:

DateTime now = DateTime.Today

like i explained earlier...

i just need a way to get the last day of the month.

thank you.
Posted
Updated 25-Jul-16 22:38pm

How is about Google: Google for "c# Last Day of Month"[^]

You will easely find it has to be something like this:
C#
DateTime today = DateTime.Today;
int lastDay = DateTime.DaysInMonth(today.Year, today.Month);
 
Share this answer
 
Comments
Richard MacCutchan 26-Jul-16 5:10am    
What a pity these so called developers seem incapable of even looking at the documentation.
Karthik_Mahalingam 26-Jul-16 5:25am    
counter 5
If date is provided you can get last day in the month by below code.

public static DateTime GetLastDayOfMonth(this DateTime dateTime)
{
    return new DateTime(dateTime.Year, dateTime.Month, DateTime.DaysInMonth(dateTime.Year, dateTime.Month));
}
 
Share this answer
 
Comments
Karthik_Mahalingam 26-Jul-16 5:25am    
5!better one
C#
// To get Last Day
       DateTime today = DateTime.Now;
       int lastDay = DateTime.DaysInMonth(today.Year, today.Month); //31


       // To Get Last Date
       DateTime firstDate = new DateTime(today.Year, today.Month, 1);
       DateTime lastDate = firstDate.AddMonths(1).AddDays(-1); // {07/31/2016 00:00:00}
 
Share this answer
 
v2
Comments
[no name] 26-Jul-16 5:14am    
+5 :)
Karthik_Mahalingam 26-Jul-16 5:25am    
Thank you :)

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