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 than | Less than | Month | Name |
---|---|---|---|
000 | 031 | 0 | January |
031 | 059 + InLeapYear(t) | 1 | February |
059 + InLeapYear(t) | 090 + InLeapYear(t) | 2 | March |
090 + InLeapYear(t) | 120 + InLeapYear(t) | 3 | April |
120 + InLeapYear(t) | 151 + InLeapYear(t) | 4 | May |
151 + InLeapYear(t) | 181 + InLeapYear(t) | 5 | June |
181 + InLeapYear(t) | 212 + InLeapYear(t) | 6 | July |
212 + InLeapYear(t) | 243 + InLeapYear(t) | 7 | August |
243 + InLeapYear(t) | 273 + InLeapYear(t) | 8 | September |
273 + InLeapYear(t) | 304 + InLeapYear(t) | 9 | October |
304 + InLeapYear(t) | 334 + InLeapYear(t) | 10 | November |
334 + InLeapYear(t) | 365 + InLeapYear(t) | 11 | December |
Note that MonthFromTime(0) is 0 which corresponds to Thursday, 01-January-1970.
// 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; }
Prev | Home | Next |
Money | Up | Month number |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |