Search code examples
javascriptnode.jsejsembedded-javascript

How to convert EJS Template's table column's strings in dd-mm-yyyy Formats?


I am rendering through EJS template like this and table data coming from Database

<td><%= Patient.StudyDate %></td>

Patient.StudyDate is a string and rendering as 20181029 (first 4 is a year then 2 is a month and last 2 is a day so I want to change this string to dd-mm-yyyy) and it's coming from Database

enter image description here

I want to show that like this 29-10-2018

How to do that in EJS templating language?


Solution

  • <td><%= Patient.StudyDate.toString().replace(/^(\d{4})(\d{2})(\d{2})$/, '$3-$2-$1')%></td>
    

    Use regexp to match days, month and year and put it in correct order

    // Patient.StudyDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$3-$2-$1');
    console.log('20181029'.replace(/^(\d{4})(\d{2})(\d{2})$/, '$3-$2-$1'));