Skip to content

Instantly share code, notes, and snippets.

@stefanoc
Created June 6, 2011 15:07
Show Gist options
  • Save stefanoc/1010432 to your computer and use it in GitHub Desktop.
Save stefanoc/1010432 to your computer and use it in GitHub Desktop.
Simple Calendar
Calendar = Class.create({
MONTHS : ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"],
DAYS : ["dom", "lun", "mar", "mer", "gio", "ven", "sab"],
MONTH_DAYS : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
year : null,
month : null,
specialDates : null,
initialize : function(_special) {
var _today = new Date()
this.year = 1900 + _today.getYear()
this.month = _today.getMonth()
this.specialDates = $A(_special)
},
getCurrentMonth : function() {
var _days = this.MONTH_DAYS[this.month]
var _date = new Date(this.year, this.month, 1)
var _day = _date.getDay()
var _days = $A()
for (var _i=0; _i<_day; _i++) {
_days.push(null)
}
for (var _i=1; _i<=this.MONTH_DAYS[this.month]; _i++) {
_days.push(_i)
}
for (var _i=this.MONTH_DAYS[this.month] + _day; _i<35; _i++) {
_days.push(null)
}
return _days
},
goToNextMonth : function() {
this.month += 1
if (this.month > 11) {
this.month = 0
this.year += 1
}
this.adjustForLeapYear()
},
goToPrevMonth : function() {
this.month -= 1
if (this.month < 0) {
this.month = 11
this.year -= 1
}
this.adjustForLeapYear()
},
adjustForLeapYear : function() {
if (this.year % 4 == 0 && !(this.year % 100 == 0 && this.year % 400 != 0))
this.MONTH_DAYS[1] = 29
else
this.MONTH_DAYS[1] = 28
},
display: function() {
console.log('DOM LUN MAR MER GIO VEN SAB')
this.getCurrentMonth().inGroupsOf(7).each(function(week) {
var _s = ''
week.each(function(day) {
if (day == null) {
_s += ' '
} else {
if (this.isSpecial(day))
_s += '*'
else
_s += ' '
_s += day
if (day < 10)
_s += ' '
}
_s += ' '
}, this)
console.log(_s)
}, this)
},
isSpecial : function(day) {
return this.specialDates.any(function(s) {
return (1900 + s.getYear()) == this.year && s.getMonth() == this.month && s.getDate() == day
}, this)
}
})
c = new Calendar([new Date()])
c.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment