Skip to content

Instantly share code, notes, and snippets.

@hirokihokari
Last active February 12, 2022 12:43
Show Gist options
  • Save hirokihokari/fd5dbe02638405857d799dcc6817f5a8 to your computer and use it in GitHub Desktop.
Save hirokihokari/fd5dbe02638405857d799dcc6817f5a8 to your computer and use it in GitHub Desktop.
const getWeekStart = (date: Date=new Date(), mondayStart: boolean=false): Date => {
const offset = mondayStart ? 1 : 0
date.setDate(date.getDate() - date.getDay() +offset)
date.setHours(0,0,0,0)
return new Date(date)
}
const getWeekEnd = (date: Date=new Date(), mondayStart: boolean=false): Date => {
const offset = mondayStart ? 1 : 0
date.setDate(date.getDate() - date.getDay() + 6 + offset)
date.setHours(23,59,59,59)
return new Date(date)
}
const getMonthStart = (date: Date=new Date()): Date => {
date.setDate(1)
date.setHours(0,0,0,0)
return new Date(date)
}
const getMonthEnd = (date: Date=new Date()): Date => {
date.setMonth(date.getMonth()+1)
date.setDate(0)
date.setHours(23,59,59,59)
return new Date(date)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment