Reputation: 143
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
Upvotes: 5
Views: 8765
Reputation: 8876
Try the following:
DateTime objDate = default(DateTime);
objDate = Convert.ToDateTime("2009-02-05");
Response.Write(DateTime.DaysInMonth(objDate.Year, objDate.Month));
Upvotes: 0
Reputation: 4876
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.CurrentUICulture;
Double DaysInMonth = cultureInfo.Calendar.GetDaysInMonth(DateTime.Now.Month, DateTime.Now.Year);
Upvotes: 0
Reputation: 839114
int daysInMonth = System.DateTime.DaysInMonth(objDate.Year, objDate.Month);
Upvotes: 3
Reputation: 36111
int noOfDays = DateTime.DaysInMonth(objDate.Year, objDate.Month);
Upvotes: 13