-2

How to convert the date format '23 MAY 2022' into 23/05/2022 in javascript?

3 Answers3

1

check this out it might help: https://stackoverflow.com/a/60858473/17757846

const newDate = new Date("23 MAY 2022").toLocaleDateString("en-GB", {
  year: "numeric",
  month: "2-digit",
  day: "2-digit",
});;

Eisa Rezaei
  • 528
  • 1
  • 12
0

I personally use momentjs

momentjs format syntax

for your case: moment('23 MAY 2022').format("DD/MM/YYYY")

Ali Sattarzadeh
  • 3,220
  • 1
  • 6
  • 20
0

I'd suggest trying luxon, an evolution of moment.js.

You can use DateTime.fromFormat() to parse the input string, then use DateTime.toFormat() to output the desired result:

const { DateTime } = luxon;

const input = '23 MAY 2022';
const dt = DateTime.fromFormat(input, 'dd MMM yyyy');
const result = dt.toFormat('dd/MM/yyyy');

console.log('Result:', result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40