Skip to content

Instantly share code, notes, and snippets.

@jesusalber1
Last active November 9, 2016 20:38
Show Gist options
  • Save jesusalber1/bcba33a9e615fbac2ac252dcc8ecf4cb to your computer and use it in GitHub Desktop.
Save jesusalber1/bcba33a9e615fbac2ac252dcc8ecf4cb to your computer and use it in GitHub Desktop.
Get range of the current week [Monday-Sunday]
/* Based on: http://stackoverflow.com/a/8381494 */
/* Date proptotype extension */
Date.prototype.getWeek = function() {
/* I get the current date and then shift it */
var today = new Date(this.setHours(0, 0, 0, 0)); /* Current date */
var dayOfWeek = today.getDay() - 1; /* It starts on Monday (Sunday by default) */
var dayOfMonth = today.getDate() - dayOfWeek; /* ([1-31]) Beginning of the week */
/* Get range */
var start = new Date(today.setDate(dayOfMonth)); /* Monday */
today.setHours(23, 59, 59, 59); /* Until the end of the day */
var end = new Date(today.setDate(dayOfMonth + 6)); /* Sunday */
/* Result as an object */
return {
start: start,
end: end
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment