Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created September 9, 2016 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherhill/97002d25f59523e5a0bf7e0373b9ad27 to your computer and use it in GitHub Desktop.
Save christopherhill/97002d25f59523e5a0bf7e0373b9ad27 to your computer and use it in GitHub Desktop.
// calendar
class Appointment {
constructor(dateStamp, description, id) {
this.id = id;
this.date = dateStamp || new Date();
this.description = description || 'New Appointment';
}
}
class Calendar {
constructor(year) {
this.isLeap = new Date(year, 2, 0).getDate() == 29;
this.appointments = [];
}
getEventsByDate(m, d, y) {
let dateStamp = new Date(y, m - 1, d);
return this.appointments.filter(
appointment => appointment.date.toDateString() === dateStamp.toDateString()
);
}
getEventsByMonth(m, y) {
let upperBound = new Date(y, m);
let lowerBound = new Date(y - 1, m);
return this.appointments.filter(
appointment => appointment.date < upperBound && appointment.date >= lowerBound
);
}
getEventsByYear(y) {
let upperBound = new Date(y, 0, 1);
let lowerBound = new Date(y, 11, 31);
return this.appointments.filter(
appointment => appointment.date < upperBound && appointment.date >= lowerBound
);
}
getEventById(id) {
return this.appointments.filter(appointment => appointment.id === id);
}
addEvent(m, d, y, id, desc) {
let dateStamp = new Date(y, m - 1, d);
return this.appointments.push(new Appointment(dateStamp, desc, id));
}
}
var ChrisCalendar = new Calendar(2016);
ChrisCalendar.addEvent(1,13,2017, 'orchestra', 'Performance');
ChrisCalendar.addEvent(4,14,1978, 'birthday', 'Chris and Grandpa Birthday');
console.log(ChrisCalendar.getEventsByYear(1978));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment