1

I am generating report i.e pdf from node to front end. I need to get a date with the format of dd/MM/yyyy . I used this :

<td style="font-size:11px;text-align: center;">
        <strong>
        <%= new Date().toISOString().slice(0, 10);%>
        </strong>
</td>

to get a date but the report show in the format of yyyy-mm-dd. I need a perfect solution to get the date format as dd/MM/yyyy.

Viveka
  • 175
  • 2
  • 12
  • 1
    Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – hlg Dec 28 '18 at 06:50

1 Answers1

1

As the best solution, I would recommend you to use moment.js. example above:

const moment = require('moment');
const formattedDate = moment().format('DD/MM/YYYY');

If you want to leave the logic with pure JS, you can do this like following:

const date = new Date().toISOString().slice(0, 10);
const [yyyy,mm,dd] = date.split('-');
const formattedDate = `${dd}/${mm}/${yyyy}`;
console.log(formattedDate);
Igor Litvinovich
  • 2,436
  • 15
  • 23