Days in year (Time calculation)

A date and time algorithm.

Availability:

ECMAScript edition - 2
Property/method value type:Number primitive

The number of days in a year depends on whether it is a leap year or not.

ECMA compliant implementations use an extended Gregorian system to map a day number to a year number and to determine the month and date within that year. In this system, leap years are summarized thus:

DaysInYear(y) =

365 if ((y modulo 4) != 0)

366 if ((y modulo 4) == 0) and ((y modulo 100) != 0)

365 if ((y modulo 4) == 0) and ((y modulo 400) != 0)

366 if ((y modulo 400) == 0)

Example code:

   // Test number of days in year

   document.write("<TABLE BORDER=1>");

   for(var ii=1980; ii<2009; ii++)

   {

   document.write("<TR>");

   document.write("<TD>");

   document.write(ii);

   document.write("</TD>");

   document.write("<TD>");

   document.write(daysInYear(ii));

   document.write("</TD>");

   document.write("</TR>");

   }

   document.write("</TABLE>");

   // Day from year function

   function daysInYear(aYear)

   {

   if((aYear % 4) != 0)

   {

   return 365;

   }

   

   if(((aYear % 100) != 0) ||

   ((aYear % 400) == 0))

   {

   return 366;

   }

   

   return 365;

   }

See also:Broken down time, Day from year, In leap year, Time range, Year number

Cross-references:

ECMA 262 edition 2 - section - 15.9.1.3

ECMA 262 edition 3 - section - 15.9.1.3