RatiboR1978
@RatiboR1978

Как установить время у понедельника текущей недели равным 00:00?

Здравствуйте, определяю понедельник текущей недели таким образом
let today = new Date();
let dayOfWeek = today.getDay();
let monday = new Date(today);
monday.setDate(today.getDate() - dayOfWeek + 1);
let mon = new Date(monday);
mon.setHours(0,0,0,0);


mon.setHours(0,0,0,0); пытаюсь выставить время 00.00, но почему то не показывает понедельник 00.00, что не так я делаю?
  • Вопрос задан
  • 58 просмотров
Решения вопроса 1
RatiboR1978
@RatiboR1978 Автор вопроса
// Get the current date
let currentDate = new Date();

// Get the current day of the week (0-6, where 0 is Sunday)
let currentDay = currentDate.getDay();

// Calculate the number of days to subtract to get to Monday (assuming Monday is the first day of the week)
let daysToMonday = currentDay === 0 ? 6 : currentDay - 1;

// Subtract the number of days to get to Monday
let mondayDate = new Date(currentDate.getTime() - (daysToMonday * 24 * 60 * 60 * 1000));

// Set the time of the date to 00:00 UTC
mondayDate.setUTCHours(0, 0, 0, 0);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
monday = new Date();
monday.setDate(monday.getDate() - monday.getDay() + 1);
monday.setHours(0, 0, 0, 0);
console.log(monday);
// Date Mon Oct 09 2023 00:00:00 GMT+0300 (Москва, стандартное время)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы