57 lines
1.2 KiB
JavaScript
Executable File
57 lines
1.2 KiB
JavaScript
Executable File
function formatDate(date) {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
function parseDateInput(dateString) {
|
|
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(dateString ?? ''));
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
|
|
const [, yearText, monthText, dayText] = match;
|
|
const year = Number(yearText);
|
|
const month = Number(monthText);
|
|
const day = Number(dayText);
|
|
const date = new Date(year, month - 1, day);
|
|
|
|
if (
|
|
date.getFullYear() !== year ||
|
|
date.getMonth() !== month - 1 ||
|
|
date.getDate() !== day
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
return date;
|
|
}
|
|
|
|
function getWeekRange(dateString) {
|
|
const baseDate = parseDateInput(dateString);
|
|
if (!baseDate) {
|
|
return null;
|
|
}
|
|
|
|
const day = baseDate.getDay();
|
|
const diffToMonday = day === 0 ? -6 : 1 - day;
|
|
|
|
const start = new Date(baseDate);
|
|
start.setDate(baseDate.getDate() + diffToMonday);
|
|
|
|
const end = new Date(start);
|
|
end.setDate(start.getDate() + 6);
|
|
|
|
return {
|
|
startDate: formatDate(start),
|
|
endDate: formatDate(end),
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
formatDate,
|
|
getWeekRange,
|
|
parseDateInput,
|
|
};
|