var Calendar = function () { var elems = document.getElementsByTagName('meta'), d = new Date; this.year = d.getFullYear(); this.month = d.getMonth() + 1; for (var i = 0; i < elems.length; i++) { var ele = elems[i], y = ele.getAttribute('year'), m = ele.getAttribute('month'); if (y) this.year = parseInt(y); if (m) this.month = parseInt(m); } }; Calendar.prototype.prepareContainer = function () { var id = 'calendar', d = document.getElementById(id); if (d) return; var d = document.createElement('div'); d.id = id; document.body.appendChild(d); }; Calendar.prototype.displayYearMonth = function () { var d = document.createElement('div'); d.textContent = this.year + '/' + this.month; d.className = 'year month'; document.getElementById('calendar').appendChild(d); }; Calendar.prototype.displayDays = function () { var f = document.createDocumentFragment(), dow = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; var nextMonth = this.month % 12 + 1, nextMonthPre = nextMonth < 10 ? '0' : '', nextYear = this.year + (this.month === 12 ? 1 : 0), nextMonth1Day = new Date(nextYear + '-' + nextMonthPre + nextMonth + '-01'), tmpTime = nextMonth1Day.getTime() - 24 * 60 * 60 * 1000, tmpDate = new Date; tmpDate.setTime(tmpTime); for (var i = 1; i <= tmpDate.getDate(); i++) { var n = document.createElement('div'), m = (this.month < 10 ? '0' : ''), day = (i < 10 ? '0' : '') + i; tmp = new Date(this.year + '-' + m + this.month + '-' + day), yb = dow[tmp.getDay()]; n.textContent = i + ' ' + yb; n.className = 'days ' + yb; f.appendChild(n); } document.getElementById('calendar').appendChild(f); }; var c = new Calendar(); c.prepareContainer(); c.displayYearMonth(); c.displayDays();