1

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?

ArunPratap
  • 4,816
  • 7
  • 25
  • 43

1 Answers1

4
<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'));
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31