tags:

views:

320

answers:

6

In C# ,How can i find the number of days in a month which resides in a DateTime object.

Ex :

DateTime objDate=new DateTime();

using the objDate, now i want to get the number of days of the current month. IS there any built-in function present in C# ?

Leap years also has to be taken care of

+7  A: 
int noOfDays = DateTime.DaysInMonth(objDate.Year, objDate.Month);
Fermin
Bah! Just beat me.
GenericTypeTea
*sigh*... I think I need to start just posting and not checking if it is correct :p
Svish
+1 First Answer
GenericTypeTea
+3  A: 
int days = DateTime.DaysInMonth(objDate.Year, objDate.Month);
GenericTypeTea
+1  A: 

How about this?

DateTime.DaysInMonth(date.Year, date.Month);
Svish
+3  A: 
int daysInMonth = System.DateTime.DaysInMonth(objDate.Year, objDate.Month);
Mark Byers
Missing a semi colon there mate.
GenericTypeTea
Thanks - added it.
Mark Byers
A: 

System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CurrentUICulture;
Double DaysInMonth = cultureInfo.Calendar.GetDaysInMonth(DateTime.Now.Month, DateTime.Now.Year);

Jeff Widmer
A: 

Try the following:

DateTime objDate = default(DateTime);
    objDate = Convert.ToDateTime("2009-02-05");

    Response.Write(DateTime.DaysInMonth(objDate.Year, objDate.Month));
Himadri