Last active
November 9, 2016 20:38
-
-
Save jesusalber1/bcba33a9e615fbac2ac252dcc8ecf4cb to your computer and use it in GitHub Desktop.
Get range of the current week [Monday-Sunday]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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