51bf195664
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import type { VisibleDateRange } from '../types'
|
|
|
|
export function startOfLocalDay(date: Date): Date {
|
|
const start = new Date(date)
|
|
start.setHours(0, 0, 0, 0)
|
|
return start
|
|
}
|
|
|
|
export function shiftLocalDays(date: Date, days: number): Date {
|
|
const shifted = new Date(date)
|
|
shifted.setDate(shifted.getDate() + days)
|
|
return shifted
|
|
}
|
|
|
|
export function shiftLocalMonths(date: Date, months: number): Date {
|
|
const shifted = new Date(date)
|
|
const targetDay = shifted.getDate()
|
|
|
|
shifted.setDate(1)
|
|
shifted.setMonth(shifted.getMonth() + months)
|
|
|
|
const lastDayOfTargetMonth = new Date(
|
|
shifted.getFullYear(),
|
|
shifted.getMonth() + 1,
|
|
0,
|
|
).getDate()
|
|
shifted.setDate(Math.min(targetDay, lastDayOfTargetMonth))
|
|
|
|
return shifted
|
|
}
|
|
|
|
export function daysVisibleRange(date: Date, days: number): VisibleDateRange {
|
|
const start = startOfLocalDay(date)
|
|
const end = shiftLocalDays(start, Math.max(1, days))
|
|
|
|
return {
|
|
startMs: start.getTime(),
|
|
endMs: end.getTime(),
|
|
}
|
|
}
|
|
|
|
export function monthGridVisibleRange(date: Date): VisibleDateRange {
|
|
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1)
|
|
const gridStart = new Date(firstDay)
|
|
gridStart.setDate(gridStart.getDate() - gridStart.getDay())
|
|
|
|
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0)
|
|
const weeks = Math.ceil((firstDay.getDay() + lastDay.getDate()) / 7)
|
|
const gridEnd = shiftLocalDays(gridStart, weeks * 7)
|
|
|
|
return {
|
|
startMs: gridStart.getTime(),
|
|
endMs: gridEnd.getTime(),
|
|
}
|
|
}
|