How to convert the date format '23 MAY 2022' into 23/05/2022 in javascript?
Asked
Active
Viewed 839 times
-2
-
use moment library – Nikhil G May 25 '22 at 10:42
-
2@Mr.Gandhi MomentJS is no longer recommended by the MomentJS team. – phuzi May 25 '22 at 10:48
-
1Why do you want to convert 23 to 25? – Adam Orłowski May 25 '22 at 10:52
-
@AdamOrłowski I am sorry It was an typing error. – Aniket Singh May 25 '22 at 12:55
3 Answers
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
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