Created
February 20, 2010 22:13
-
-
Save bloudermilk/309946 to your computer and use it in GitHub Desktop.
Javascript Date prototypes for finding the beginning and end of days and weeks. Beginning of the week is defined as Sunday
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::beginningOfMonth = -> | |
beginningOfMonth = @beginningOfDay() | |
beginningOfMonth.setDate(1) | |
beginningOfMonth | |
Date::endOfMonth = -> | |
endOfMonth = @endOfDay() | |
endOfMonth.setMonth(endOfMonth.getMonth() + 1) | |
endOfMonth.setDate(0) | |
endOfMonth | |
Date::endOfWeek = -> | |
daysToSaturday = if @getDay() != 6 then 6 - @getDay() else 0 | |
endOfWeek = new Date(@) | |
endOfWeek.setDate(@getDate() + daysToSaturday) | |
endOfWeek.endOfDay() | |
Date::beginningOfWeek = -> | |
daysToSunday = if @getDay() != 0 then @getDay() else 0 | |
beginningOfWeek = new Date(@) | |
beginningOfWeek.setDate(@getDate() - daysToSunday) | |
beginningOfWeek.beginningOfDay() | |
Date::endOfDay = -> | |
new Date(@getFullYear(), @getMonth(), @getDate(), 23, 59, 59, 999) | |
Date::beginningOfDay = -> | |
new Date(@getFullYear(), @getMonth(), @getDate(), 0, 0, 0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment