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

158
Visualizações
invalid date when trying to change the date format

I have a date value and want to format it.

This is the value:

Thu Jul 07 2022 01:03:18 GMT+0200 (Mitteleuropäische Sommerzeit) 

I want to make this format:

console.log(new Date('YYYY-MM-DD h:i:s', dateVal));

but I get invalid date.

How can I make it right ?

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

0

Though the Date object's constructor accepts a handful of parameters to set an actual date, there is no parameter to specify in what way the date should be presented.

So:

let dateVal="Thu Jul 07 2022 01:03:18 GMT+0200 (Mitteleuropäische Sommerzeit)";
console.log(new Date('YYYY-MM-DD h:i:s', dateVal));

is nonsense as it tries to construct a date from two strings.

What you can do however is call the Date's built-in function .toLocaleString() including the "sv" locale to get your Date formatted in YYYY-MM-DD h:m:s format.

For example:

let dateVal = "Thu Jul 7 2022 01:03:18 GMT+0200 (Mitteleuropäische Sommerzeit)";
console.log(new Date(dateVal).toLocaleString("sv"));

about 4 years ago · Juan Pablo Isaza Relatório

0

The Date constructor takes only one argument, you can't format a date using it. Do you want exact format you've shown in your question or an ability to use any arbitrary date format? For the first option, the simplest way I see so far is:

let dateVal = 'Thu Jul 17 2022 01:03:18 GMT+0200 (Mitteleuropäische Sommerzeit)';
let dateObj = new Date(dateVal);
let dateISO = dateObj.toISOString()
let dateFmt = dateISO.slice(0, 10) + ' ' + dateISO.slice(11, 19)
console.log(dateFmt)

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