Search code examples
javascriptdatedatetime-format

How to format date in javascript as DD-MMM-YYYY?


So the backend serves date to FE as: '2019-05-30T00:00:00.000Z'

I am trying render on FE as 30-May-2019

So...

let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') );

I have been trying to use the MDN Javascript Date documentation

But how do you even start to modify a Date object and control to desired output?


Solution

  • I managed to create a working function for this:

    function formatDate(value) {
        let date = new Date(value);
        const day = date.toLocaleString('default', { day: '2-digit' });
        const month = date.toLocaleString('default', { month: 'short' });
        const year = date.toLocaleString('default', { year: 'numeric' });
        return day + '-' + month + '-' + year;
    }
    
    formatDate('2019-05-30T00:00:00.000Z') // 30-May-2019