user198880
user198880

Reputation: 143

C# : How to get number of days from an object of DateTime

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

Answers (6)

Himadri
Himadri

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

Jeff Widmer
Jeff Widmer

Reputation: 4876

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

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 839114

int daysInMonth = System.DateTime.DaysInMonth(objDate.Year, objDate.Month);

Upvotes: 3

Svish
Svish

Reputation: 158321

How about this?

DateTime.DaysInMonth(date.Year, date.Month);

Upvotes: 1

djdd87
djdd87

Reputation: 68506

int days = DateTime.DaysInMonth(objDate.Year, objDate.Month);

Upvotes: 3

Fermin
Fermin

Reputation: 36111

int noOfDays = DateTime.DaysInMonth(objDate.Year, objDate.Month);

Upvotes: 13

Related Questions