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

287
Visualizações
transforming a date in Month day, YEAR format to YYYY-MM-DD format?

Is there a way to reformat a date using .replace and a regex? or a series of them?

for example I would like to turn May 4, 1981 into 1981-May-04... this would be good enough for my needs.

but even better would be to turn May 4, 1981 into 1981-05-04.

please note that single digit months and dates need to be changed to double digit (i.e. prefix a 0). The source text May 4, 1981 would not contain a leading 0 for the day ever.

the end result YYYY-MM-DD being sortable, which is why the leading 0 is important.

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

0

new Date('May 4, 1981').toLocaleDateString('en-CA') outputs '1981-05-04'

toLocaleDateString is pretty powerful - check it out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Alternatives:

  1. Write your own simple function to extract the date data, irrespective of the locale.

const date = new Date('May 4, 1981');

function getMyFormat(date) {
  return [
      date.getFullYear(), 
      date.getMonth().toString().padStart(2, '0'),
      date.getDay().toString().padStart(2, '0')
  ].join('-');
}

console.log(getMyFormat(date))

  1. Use the ISO string (standardized format of time representation here)
new Date(date).toISOString().split('T')[0]

It spits out a long string where, before the 'T', the format is YYYY-MM-DD.

There are some caveats to this, however, as it puts the date in the UTC timezone, which could affect your setup.

There is, however, an easy way to handle this:

new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000).toISOString().split('T')[0]

Over here, we calculate the timezone offset with respect to UTC so it should help you get the format you need.

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