I
I
Im p3l2021-07-05 10:41:10
JavaScript
Im p3l, 2021-07-05 10:41:10

How to get the day of the week and number using new Date()?

Good afternoon, I want to get tomorrow and the day after tomorrow in this format "Day of the week, day of the month". For example: "Tuesday 6 July", "Wednesday 7 July"

const date = new Date();
const tomorrow = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate() + 1}`
const dayAfterTomorrow = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate() + 2}`

const tomorrowString = tomorrow.toLocaleString('ru', { month: 'long', weekday: 'long' })
const dayAfterTomorrowString = dayAfterTomorrow.toLocaleString('ru', { month: 'long', weekday: 'long' })


But that doesn't work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-07-05
@LoranDeMarcus

const format = (date) => {
  const formatted = date.toLocaleString('ru-RU', {
    weekday: 'long',
    day: 'numeric',
    month: 'long',
  });
  
  return `${formatted[0].toUpperCase()}${formatted.slice(1)}`;
};

const today = Date.now();
const day = 24 * 60 * 60 * 1000;
const tomorrow = format(new Date(today + day));
const dayAfterTomorrow = format(new Date(today + day * 2));

console.log(tomorrow); // 'Вторник, 6 июля'
console.log(dayAfterTomorrow); // 'Среда, 7 июля'

or
const formatter = new Intl.DateTimeFormat('ru-RU', {
    weekday: 'long',
    day: 'numeric',
    month: 'long',
});

const format = (date) => {
  const formatted = formatter.format(date);
  
  return `${formatted[0].toUpperCase()}${formatted.slice(1)}`;
};

const today = Date.now();
const day = 24 * 60 * 60 * 1000;
const tomorrow = format(new Date(today + day));
const dayAfterTomorrow = format(new Date(today + day * 2));

console.log(tomorrow); // 'Вторник, 6 июля'
console.log(dayAfterTomorrow); // 'Среда, 7 июля'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question