Home > Enterprise >  how to change my this date format "20221015T000000Z" into yyyy-mm-dd (2022-10-31)?
how to change my this date format "20221015T000000Z" into yyyy-mm-dd (2022-10-31)?

Time:10-31

i want to change my date format from "20221015T000000Z" to yyyy-mm-dd. i tried looking up solutions but not able to find anything relatable.

CodePudding user response:

The example date string already contains all the information you need in clear text. It's not a standard date string, but assuming the format of the string is always going to be like that, all you have to do is to extract the substrings and stitch them back together using the - separator.

This is exactly what this function does by using String's substr method and Array's join method:

const parseDateString = s => [s.substr(0,4), s.substr(4,2), s.substr(6,2)].join('-');

Usage:

const result = parseDateString('20221015T000000Z');
// do something with result, e.g. console.log(result)
  • Related