DaysInYear...

  • Thread starter Thread starter Giri
  • Start date Start date
G

Giri

what is the easiest way of establishing this given a Year?

(short of DateTime.parsing a made up string representing 1/1 to 31/12 blah
blah blah)

Thanks


Giri
 
Giri said:
what is the easiest way of establishing this given a Year?

(short of DateTime.parsing a made up string representing 1/1 to 31/12 blah
blah blah)

Thanks


Giri

DateTime has a static DayInMonth method that accepts a year and a month.
You could loop through all 12 months for a given year, or just check the
number of days in februari to select either 365 or 366 days.

Hans Kesting
 
Hans and Giri,

That seems much, much longer than this:

public static int DaysInYear(DateTime value)
{
// Get the year. Construct dates for New Year's Day, and subtract.
int pintYear = value.Year;

// Now construct dates and subtract from each other to get the return
value.
int pintRetVal = (new DateTime(pintYear + 1, 1, 1) - new
DateTime(pintYear, 1, 1)).Days;


// That's all folks.
return pintRetVal;
}

Hope this helps.
 
private int DaysInYear(int year)
{
return System.DateTime.IsLeapYear(year) ? 366 : 365;
}
 
Giri said:
what is the easiest way of establishing this given a Year?

(short of DateTime.parsing a made up string representing 1/1 to 31/12 blah
blah blah)

Thanks


Giri

This will take into account leap years, calendars of other cultures, etc.:

using System.Globalization;

// ...

int days = CultureInfo.CurrentCulture.Calendar.GetDaysInYear( year);

Depending on your needs, you might want to use InvariantCulture instead
of CurrentCulture.
 
If you want to be accurate, you must get the correct instance of a
Calander-derrived class (from System.Globalization) for the region your
program is executing in. Once you have the correct calander class for the
region, you can query its GetDaysInYear method.

-Rob Teixeira [MVP]
 
Back
Top