Last active
November 25, 2017 21:35
-
-
Save jamesridgway/de716697d522f06b012b2affd751e05a to your computer and use it in GitHub Desktop.
Javascript date functions
This file contains 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
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