Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Created February 20, 2010 22:13
Show Gist options
  • Save bloudermilk/309946 to your computer and use it in GitHub Desktop.
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
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