Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
How to get nearest date ahead by day of week number?

I'm new to moment and I need to get the nearest date ahead by the number of the day of the week.

Depending on the current date, I need to get, for example:

const currentWeekDay = '4'; (14.04)
const preparedAlternativeDates = [
  { DAY: 1, VALUE: '18.04' },
  { DAY: 2, VALUE: '19.04'},
  { DAY: 3, VALUE: '20.04'},
  { DAY: 4, VALUE: '21.04'},
  { DAY: 5, VALUE: '15.04'},
  { DAY: 6, VALUE: '16.04'},
  { DAY: 7, VALUE: '17.04'},
];

I'm trying to use the script below but it doesn't work. Maybe there is an easier way to do this?

const ALTERNATIVE_DATES = Object.freeze([
  { DAY: 1, VALUE: null },
  { DAY: 2, VALUE: null },
  { DAY: 3, VALUE: null },
  { DAY: 4, VALUE: null },
  { DAY: 5, VALUE: null },
  { DAY: 6, VALUE: null },
  { DAY: 7, VALUE: null },
]);

const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
  if (!alternativeDate.VALUE) {
    const alternativeDateDay = Number(alternativeDate.DAY);
    const currentWeekDay = Number(moment().isoWeekday());

    if (alternativeDateDay <= currentWeekDay) {
      alternativeDate.VALUE = moment().add(alternativeDateDay + currentWeekDay - 1, 'd').startOf('day');
    } else {
      alternativeDate.VALUE = moment().add(alternativeDateDay - currentWeekDay, 'd').startOf('day');
    }
  }

  return {
    day: alternativeDate.DAY,
    value: alternativeDate.VALUE
  };
});

I will be grateful for any help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I just adjusted your code and tested the result. this should work for you:

const ALTERNATIVE_DATES = Object.freeze([
     { DAY: 1, VALUE: null },
     { DAY: 2, VALUE: null },
     { DAY: 3, VALUE: null },
     { DAY: 4, VALUE: null },
     { DAY: 5, VALUE: null },
     { DAY: 6, VALUE: null },
     { DAY: 7, VALUE: null },
  ]);

  const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
     if (!alternativeDate.VALUE) {
        const wishedDay = Number(alternativeDate.DAY);
        const today = moment().isoWeekday();

        if (today < wishedDay) {
           alternativeDate.VALUE = moment().isoWeekday(wishedDay).format('DD.MM');
        } else {
           alternativeDate.VALUE = moment().add(1, 'weeks').isoWeekday(wishedDay).format('DD.MM');
        }
     }

     return {
        day: alternativeDate.DAY,
        value: alternativeDate.VALUE,
     };
  });

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda