Skip to content

Instantly share code, notes, and snippets.

@tobi
Created July 14, 2011 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobi/1081845 to your computer and use it in GitHub Desktop.
Save tobi/1081845 to your computer and use it in GitHub Desktop.
Quick access to useful timespans for reporting
selectors: ->
today = new Date()
today.setMinutes(0)
today.setHours(0)
today.setSeconds(0)
day = 1000*60*60*24
yesterday = today - day
lastSunday = today - today.getDay() * day
firstOfMonth = (date) ->
date - day * new Date(date).getDate() + day
beginningOfMonth = firstOfMonth(today)
endOfMonth = firstOfMonth(beginningOfMonth + 31 * day) - day
endOfLastMonth = beginningOfMonth - day
beginningOfLastMonth = firstOfMonth(endOfLastMonth)
return {
"Yesterday": [yesterday, yesterday],
"Last 7 days": [yesterday - 7 * day, yesterday]
"This Week": [lastSunday, lastSunday + 7 * day]
"Last Week": [lastSunday - 7 * day, lastSunday]
"This Month": [beginningOfMonth, endOfMonth]
"Last Month": [beginningOfLastMonth, endOfLastMonth]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment