Skip to content

Instantly share code, notes, and snippets.

@jamesridgway
Last active November 25, 2017 21:35
Show Gist options
  • Save jamesridgway/de716697d522f06b012b2affd751e05a to your computer and use it in GitHub Desktop.
Save jamesridgway/de716697d522f06b012b2affd751e05a to your computer and use it in GitHub Desktop.
Javascript date functions
Date.prototype.asStartOfWeek = function(start) {
start = start || 0;
var today = new Date(this.setHours(0, 0, 0, 0));
var day = today.getDay() - start;
var date = today.getDate() - day + (day == 0 ? -6 : 1);
return new Date(today.setDate(date));
}
Date.prototype.asEndOfWeek = function(start) {
start = start || 0;
var today = new Date(this.setHours(23, 59, 59, 999));
var day = today.getDay() - start;
var date = today.getDate() - day + (day == 0 ? -6 : 1);
return new Date(today.setDate(date + 6));
}
Date.prototype.withinTheWeekOf = function(weekDate) {
return this.getTime() >= weekDate.asStartOfWeek().getTime() && this.getTime() <= weekDate.asEndOfWeek().getTime();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment