Tofu/app/tools/dates/formatWeekRange.js

21 lines
1 KiB
JavaScript

const MONTH_FORMATTER = new Intl.DateTimeFormat('fr-FR', { month: 'long' })
const DAYS_TO_SUNDAY = 6
// Returns the sheet title for the week starting at `weekStart`, for example
// "Semaine du 21 au 27 septembre". The month is repeated when the week spans two
// months, and both years are added when it spans two years.
export function formatWeekRange(weekStart) {
const weekEnd = new Date(weekStart.getTime())
weekEnd.setDate(weekEnd.getDate() + DAYS_TO_SUNDAY)
const startMonth = MONTH_FORMATTER.format(weekStart)
const endMonth = MONTH_FORMATTER.format(weekEnd)
const startYear = weekStart.getFullYear()
const endYear = weekEnd.getFullYear()
if (startYear !== endYear) {
return `Semaine du ${weekStart.getDate()} ${startMonth} ${startYear} au ${weekEnd.getDate()} ${endMonth} ${endYear}`
}
if (startMonth !== endMonth) {
return `Semaine du ${weekStart.getDate()} ${startMonth} au ${weekEnd.getDate()} ${endMonth}`
}
return `Semaine du ${weekStart.getDate()} au ${weekEnd.getDate()} ${startMonth}`
}