Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda