full-stack-school/test/test-date.js

30 lines
870 B
JavaScript

const latestMonday = new Date();
const dayOfWeek = latestMonday.getDay();
const daysSinceMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
latestMonday.setDate(latestMonday.getDate() - daysSinceMonday);
latestMonday.setHours(0, 0, 0, 0);
const dayMap = { MONDAY: 0, TUESDAY: 1, WEDNESDAY: 2, THURSDAY: 3, FRIDAY: 4 };
function adjust(dayKey) {
const targetDayOffset = dayMap[dayKey] || 0;
const targetDate = new Date(latestMonday);
targetDate.setDate(latestMonday.getDate() + targetDayOffset);
const originalStart = new Date("2026-02-27T10:00:00Z"); // specific test time
const adjustedStart = new Date(originalStart);
adjustedStart.setFullYear(
targetDate.getFullYear(),
targetDate.getMonth(),
targetDate.getDate()
);
console.log(dayKey, adjustedStart.toString());
}
adjust("MONDAY");
adjust("WEDNESDAY");
adjust("FRIDAY");