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

229
Visualizações
How to change word order from within a string?

I have a string that returns from my Backend: startDate: "September 28, 2021" in the format: mm-dd-yyyy. I need to format this string in Front to display: yyyy-mm-dd ie: 2021, September 28.

How do I format a string and change the order of the words contained in it?

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

0

You could use a regex replacement here:

var input = "September 28, 2021";
var output = input.replace(/\b(\w+ \d{1,2}), (\d{4})\b/, "$2, $1");
console.log(input);
console.log(output);

Regarding the choice of regex pattern I used, it should be sufficient and safe assuming that nothing else in your text would be a word followed by a 1-2 digit number, comma, then a 4 digit number.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is the easy one that will also validate your date too

let date = new Date("September 28, 2021");
let month = date.toLocaleString("default", { month: "long" });

let fomattedDate = `${date.getFullYear()}, ${month} ${date.getDate()}`;

console.log(fomattedDate);
about 4 years ago · Juan Pablo Isaza Relatório

0

RegEx works, but if you want to be able to read and understand the code:

const input = 'September 28, 2021';
const [month, day, year] = input.split(' ');
const formattedDate = `${year}, ${month} ${day.replace(',', '')}`
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