Tofu/app/tools/dates/getWeekStart.js

10 lines
495 B
JavaScript

// Returns the Monday 00:00:00 (local time) of the week containing `date`.
// The week starts on Monday: a Sunday belongs to the week that started six days earlier.
export function getWeekStart(date) {
const weekStart = new Date(date.getTime())
weekStart.setHours(0, 0, 0, 0)
// getDay() is 0 for Sunday and 1 for Monday: rotate it so Monday becomes 0 and Sunday 6.
const dayOffset = (weekStart.getDay() + 6) % 7
weekStart.setDate(weekStart.getDate() - dayOffset)
return weekStart
}