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
Output the day of the week, delivers me 2x the same day

I want to display the days of the week based on the days in a month. Now it is the case that Sunday is always counted twice. However, when I open the debugger and set a breakpoint in the for loop, the "0" for Sunday is only counted once. How so ?

enter image description here

function getDayName(dateStr, locale) {
var date = new Date(dateStr);
return date.toLocaleDateString(locale, { weekday: "long" });
}

const Items = () => {
let array = [];
const d = new Date();
let tageImMonat = daysInMonth(d.getMonth() + 1, d.getFullYear());

for (var i = 1; i <= tageImMonat; i++) {
var newDate = new Date(d.getFullYear(), d.getMonth(), i);
var day = getDayName(newDate, "de-DE");
if (newDate.getDay() === 0) {
  //if Sunday
  array.push(
    <Item sx={{ backgroundColor: "red", textAlign: "left" }}>
      {i} {day}
    </Item>
  );
}
if (newDate.getDay() === 6) {
  //if Saturday
  array.push(
    <Item sx={{ backgroundColor: "lightgrey", textAlign: "left" }}>
      {i} {day}
    </Item>
  );
} else if (newDate.getDay() !== 0 || newDate.getDay() !== 6) {
  array.push(
    <Item sx={{ textAlign: "left" }}>
      {i} {day}
    </Item>
   );
  }
}

  return array;
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try like this:

function getDayName(dateStr, locale) 
{
  var date = new Date(dateStr);
  return date.toLocaleDateString(locale, { weekday: "long" });
}

function Items()
{
  const array = [];
  const d = new Date();
  const tageImMonat = daysInMonth(d.getMonth() + 1, d.getFullYear());

  for (let i = 1; i <= tageImMonat; i++) 
  {
    const newDate = new Date(d.getFullYear(), d.getMonth(), i);
    const day = getDayName(newDate, "de-DE");
    switch (newDate.getDay())
    {
      case 0:
        //if Sunday
        array.push(
          <Item sx={{ backgroundColor: "red", textAlign: "left" }}>
            {i} {day}
          </Item>
        );
        break;
      case 6:
        //if Saturday
        array.push(
          <Item sx={{ backgroundColor: "lightgrey", textAlign: "left" }}>
            {i} {day}
          </Item>
        );
        break;
      default:
        array.push(
          <Item sx={{ textAlign: "left" }}>
            {i} {day}
          </Item>
        );
    }
  }

  return array;
}
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