# Month from time (Time calculation)

A date and time algorithm defined by ECMAScript.

Availability:

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

In an ECMA compliant implementation, given a time measured in milliseconds from 01-January-1970 UTC, the month number can be calculated from the value.

The formula for calculating day number is shown here:

Day(t) = floor(t/msPerDay), where:

t = an instant in time measured in milliseconds relative to 01-January-1970 UTC.

msPerDay = 86400000

All non leap years have 365 days with the usual number of days in each month. Leap years have an extra day in February. The calculation shown below uses known leap years and non leap years to adjust the day numbers and yield the day number of the first day of the given year and then use that to work out the time in milliseconds when the year started:

DayFromYear(y) =

365 * (y - 1970) +

floor((y - 1969) / 4) -

floor((y - 1901) / 100) +

floor((y - 1601) / 400)

TimeFromYear(y) = msPerDay * DayFromYear(y)

YearFromTime(t) = The largest integer y to make TimeFromYear(y) less than or equal to t.

DayWithinYear(t) = Day(t) - DayFromYear(YearFromTime(t))

The month value is worked out with this formulaic framework:

MonthFromTime(t) = lookup according to DayWithinYear(t) falling into a range according to the following table:

Greater thanLess thanMonthName
0000310January
031059 + InLeapYear(t)1February
059 + InLeapYear(t)090 + InLeapYear(t)2March
090 + InLeapYear(t)120 + InLeapYear(t)3April
120 + InLeapYear(t)151 + InLeapYear(t)4May
151 + InLeapYear(t)181 + InLeapYear(t)5June
181 + InLeapYear(t)212 + InLeapYear(t)6July
212 + InLeapYear(t)243 + InLeapYear(t)7August
243 + InLeapYear(t)273 + InLeapYear(t)8September
273 + InLeapYear(t)304 + InLeapYear(t)9October
304 + InLeapYear(t)334 + InLeapYear(t)10November
334 + InLeapYear(t)365 + InLeapYear(t)11December

Note that MonthFromTime(0) is 0 which corresponds to Thursday, 01-January-1970.

## Example code:

```   // Work out a month number from a time value
var msPerDay = 86400000;
var myMilliseconds = Number(new Date());
document.write(monthFromDayNumber(myMilliseconds));
// Month from day number
function monthFromDayNumber(aMillisecondTime)
{
var myMonthLookup = new Array();

var myDayWithinYear = dayWithinYear(aMillisecondTime);
var myYearFromTime  = yearFromTime(aMillisecondTime);
var myLeapNumber    = +inLeapYear(myYearFromTime);
myMonthLookup[0]  =   0;
myMonthLookup[1]  =  31;
myMonthLookup[2]  =  59 + myLeapNumber;
myMonthLookup[3]  =  90 + myLeapNumber;
myMonthLookup[4]  = 120 + myLeapNumber;
myMonthLookup[5]  = 151 + myLeapNumber;
myMonthLookup[6]  = 181 + myLeapNumber;
myMonthLookup[7]  = 212 + myLeapNumber;
myMonthLookup[8]  = 243 + myLeapNumber;
myMonthLookup[9]  = 273 + myLeapNumber;
myMonthLookup[10] = 304 + myLeapNumber;
myMonthLookup[11] = 334 + myLeapNumber;

for(var ii=0; ii<12; ii++)
{
if(myDayWithinYear < myMonthLookup[ii])
{
return ii;
}
}
return 12;
}
// Work out day number within year based on time value
function dayWithinYear(aMilliseconds)
{
var myDayNumber     = dayNumber(aMilliseconds);
var myYearFromTime  = yearFromTime(aMilliseconds);
var myDayFromYear   = dayFromYear(myYearFromTime);
var myDayWithinYear = myDayNumber - myDayFromYear;
return myDayWithinYear;
}
// Return year number based on time value
function yearFromTime(aMilliseconds)
{
var myStartYear = 1970;
while(timeFromYear(myStartYear) < myMilliseconds)
{
myStartYear++
}
return myStartYear-1;
}
// Work out milliseconds at start of year
function timeFromYear(aYear)
{
var myTime = msPerDay * dayFromYear(aYear);
return myTime;
}
// Work out day number from milliseconds
function dayNumber(aMillisecondTime)
{
var myDay = Math.floor(aMillisecondTime/msPerDay);
return myDay;
}
// Day from year function
function dayFromYear(aYear)
{
var myDay = 365 * (aYear - 1970)             +
Math.floor((aYear - 1969) / 4)   -
Math.floor((aYear - 1901) / 100) +
Math.floor((aYear - 1601) / 400);
return myDay;
}
// Flag a leap year with a Boolean value
function inLeapYear(aYear)
{
if((aYear % 4) != 0)
{
return false;
}
if(((aYear % 100) != 0) ||
((aYear % 400) == 0))
{
return true;
}
return false;
}```

 See also: Date from time, Date number, Day from year, Day number, Day within year, Month number, Year from time

## Cross-references:

ECMA 262 edition 2 - section - 15.9.1.4

ECMA 262 edition 3 - section - 15.9.1.4