Created
June 1, 2016 18:35
-
-
Save JimOKelly/2369917963a54f6965e7ab59bb0f1100 to your computer and use it in GitHub Desktop.
BEHOLD! I give you JavaScript magic like Ruby haseth!
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
(function() { | |
Date.prototype.today = function() { | |
var date = this; | |
var beginningOfMonth = function() { | |
var bom = new Date(date.getFullYear(), date.getMonth(), 1); | |
var beginningOfWeek = function() { | |
var day = bom.getDay(); | |
var diff = bom.getDate() - day - (day === 0 ? -7 : 0); | |
return new Date(bom.setDate(diff)); | |
}; | |
return { | |
beginningOfWeek: beginningOfWeek | |
}; | |
}; | |
var endOfMonth = function() { | |
var eom = new Date(date.getFullYear(), date.getMonth() + 1, 0); | |
var endOfWeek = function() { | |
var day = eom.getDay(); | |
var diff = eom.getDate() + day + (day === 0 ? 6 : -1); | |
return new Date(eom.setDate(diff)); | |
}; | |
return { | |
endOfWeek: endOfWeek | |
}; | |
}; | |
return { | |
beginningOfMonth: beginningOfMonth, | |
endOfMonth: endOfMonth | |
}; | |
}; | |
var firstCalendarDay = new Date().today().beginningOfMonth().beginningOfWeek(); | |
var lastCalendarDay = new Date().today().endOfMonth().endOfWeek(); | |
console.log(firstCalendarDay); | |
console.log(lastCalendarDay); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment