Ad

Convert Date String Of Format `YYYYMMDD` Into `DD/MM/YYYY`

- 1 answer

I have an xml that gives me the date as a text in reverse "20200528". I need to change that string in a date format DD/MM/YYYY in JavaScript.

Ad

Answer

One of possible ways to process the pattern YYYYMMDD (4-digit year, 2-digit month, 2-digit day) is using RegExp:

Following is a quick demo:

const dateStr = '20200528',
      [,yyyy,mm,dd] = dateStr.match(/(\d{4})(\d{2})(\d{2})/),
      
      result = `${dd}/${mm}/${yyyy}`
      
console.log(result)      
.as-console-wrapper{min-height:100%;}

Ad
source: stackoverflow.com
Ad