31 lines
925 B
TypeScript
31 lines
925 B
TypeScript
export function formatDateTime(
|
|
date: Date,
|
|
useDate = true,
|
|
useTime = false,
|
|
useSeconds = false,
|
|
useWeekday = false
|
|
) {
|
|
const dateTimeFormat = new Intl.DateTimeFormat([], {
|
|
year: useDate ? 'numeric' : undefined,
|
|
month: useDate ? '2-digit' : undefined,
|
|
day: useDate ? '2-digit' : undefined,
|
|
weekday: useWeekday ? 'long' : undefined,
|
|
hour: useTime ? '2-digit' : undefined,
|
|
minute: useTime ? '2-digit' : undefined,
|
|
second: useTime && useSeconds ? '2-digit' : undefined,
|
|
});
|
|
return dateTimeFormat.format(date);
|
|
}
|
|
|
|
export function asHour(date?: Date) {
|
|
if (date) return formatDateTime(date, false, true);
|
|
}
|
|
|
|
export function startOfWeek(date: Date, startMonday = true) {
|
|
const start = new Date(date);
|
|
const day = date.getDay() || 7;
|
|
if (startMonday && day !== 1) start.setHours(-24 * (day - 1));
|
|
else if (!startMonday && day !== 7) start.setHours(-24 * day);
|
|
return start;
|
|
}
|