Created
April 6, 2012 11:53
-
-
Save Saneyan/2319162 to your computer and use it in GitHub Desktop.
Create calendar method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createCalendar( year, month ){ | |
| var calendarArray = []; | |
| var weekdayStrArray = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; | |
| var monthStrArray = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; | |
| var monthArray = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; | |
| var monthDay = monthArray[ month - 1 ]; | |
| var currentDay = ( new Date( year, ( month - 1 ), 1, 0, 0, 0 ) ).getDay(); | |
| if( month == 2 ) | |
| monthDay += year % 4 ? 0 : year % 100 ? 1 : year % 400 ? 0 : 1; | |
| for( var d = 1; d <= monthDay; d++ ) | |
| calendarArray[ d + currentDay - 1 ] = d; | |
| // Body | |
| var dom_calendarBody = document.createElement( 'div' ); | |
| // Head | |
| var dom_calendarHead = document.createElement( 'table' ), | |
| dom_calendarHeadRow = document.createElement( 'tr' ), | |
| dom_calendarHeadDate = document.createElement( 'th' ); | |
| dom_calendarHeadDate.appendChild( document.createTextNode( monthStrArray[ month ] + ', ' + year ) ); | |
| dom_calendarHead.appendChild( dom_calendarHeadRow ); | |
| dom_calendarHead.appendChild( dom_calendarHeadDate ); | |
| dom_calendarBody.appendChild( dom_calendarHead ); // * Append to body | |
| // Table | |
| var dom_calendarTable = document.createElement( 'table' ), | |
| dom_calendarTableWeekday = document.createElement( 'tr' ); | |
| dom_calendarTable.appendChild( dom_calendarTableWeekday ); | |
| dom_calendarBody.appendChild( dom_calendarTable ); // * Append to body | |
| // Set weekday title | |
| var dom_day; | |
| for( var i = 0; i < 7; i++ ){ | |
| dom_day = document.createElement( 'th' ); | |
| dom_day.appendChild( document.createTextNode( weekdayStrArray[ i ] ) ); | |
| dom_calendarTableWeekday.appendChild( dom_day ); | |
| } | |
| // Set days | |
| var day, dom_week; | |
| var len = calendarArray.length; | |
| for( i = 0; i < len; i++ ){ | |
| if( i % 7 === 0 ) | |
| dom_week = document.createElement( 'tr' ); | |
| dom_day = document.createElement( 'td' ); | |
| dom_week.appendChild( dom_day ); | |
| day = calendarArray [ i ]; | |
| if( day ) | |
| dom_day.appendChild( document.createTextNode( day ) ); | |
| if( i % 7 == 6 || i + 1 == len ) | |
| dom_calendarTable.appendChild( dom_week ); | |
| } | |
| return dom_calendarBody; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment