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

168
Visualizações
Js: Unix-Date-Converter returning wrong month

I have this basic function to convert my unix timestamps into a date string. I have seen that it returns the wrong month (29-10 instead of 29-11). I was able to reproduce it on JsFiddle. What could be the problem?

  function unixTime(unixtime) {
      var u = new Date(unixtime*1000);
        return u.getUTCFullYear() +
          '-' + ('0' + u.getUTCMonth()).slice(-2) +
          '-' + ('0' + u.getUTCDate()).slice(-2)
        //  ' ' + ('0' + u.getUTCHours()).slice(-2) +
      //    ':' + ('0' + u.getUTCMinutes()).slice(-2) +
        //  ':' + ('0' + u.getUTCSeconds()).slice(-2)
      }
      
      var test = unixTime(1638178329);
      
      console.log(test);

https://jsfiddle.net/e2h7p19k/8/

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

0

You can use Date.toLocaleDateString() to format strings in the desired format. Selecting a locale of 'sv' will give you ISO dates and selecting the 'UTC' timeZone will give the correct timezone.

function unixTime(unixtime) {
    return new Date(unixtime * 1000).toLocaleDateString('sv', { timeZone: 'UTC'})
}

console.log(unixTime(1638178329))
console.log(unixTime(Date.parse('2021-05-04')/1000))
console.log(unixTime(Date.parse('2015-12-25')/1000))
.as-console-wrapper { max-height: 100% !important; top: 0; }

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