Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!