简体   繁体   中英

How to convert a date format in JavaScript?

如何在javascript中将日期格式'23 MAY 2022'转换为23/05/2022?

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",
});;

I personally use momentjs

momentjs format syntax

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM