Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to display the last date of the month based on the year (taking into account leap year)in ASP.NET
Posted
Updated 27-Oct-10 22:55pm
v2
Comments
Sunasara Imdadhusen 28-Oct-10 4:57am    
Do you want to get max day number based on Month and Year value?

Let .Net handle this for you:

public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
{
   DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
   return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
 
Share this answer
 
Comments
Sunasara Imdadhusen 28-Oct-10 5:01am    
Good Answer
raju melveetilpurayil 28-Oct-10 5:17am    
good one.
C#
public int GetDaysInMonth(int month, int year)
{
DateTime date = DateTime.Now;
if (month == 12) date = new DateTime(year + 1, 1, 1);
else date = new DateTime(year, month + 1, 1);
return date.AddDays(-1).Day;
}
 
Share this answer
 
Comments
SukanyaN 28-Oct-10 5:22am    
Thanks Mohd Wasif and Sunasara Imdadhusen for your immediate response.

I will explain my application briefly.(ASP.NET)
I place 3 DropDownLists in my design page.

DDL1-Date (1-31)
DDL2-Month (Jan-Dec)
DDL3-Year (2000-2015)
eg: When i select 2001,Mar...My date must show 31.
When i select 2004,Feb...My date must show 29.
Kindly help me.

Thanks & Regards,
Sukanya Narayanan.
saini arun 28-Oct-10 7:05am    
protected void SelectLastDateByMonthAndYear()
{
/// Convert the month & year values to int before passing them to GetDaysInMonth
int days = GetDaysInMonth(<selected value from month drop down>, <selected value from year drop down>);

ddlDate.Items.FindByText(days.ToString()).Selected = true;
}

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