Convert Epoch time to Date string

The code snippet below shows how to format epoch time to human readable date. If you have the epoch time then you can simply use the code below.

var date = new Date(1474356766254);
console.log(date.toDateString());

It returns a string as Tue Sep 20 2016. If you need to format the date into some other form then you need to do it manually. The function below does it for you.

function getFormattedDate(epochTime) {
    var monthNames = [
      'January', 'February', 'March',
      'April', 'May', 'June', 'July',
      'August', 'September', 'October',
      'November', 'December'
    ];
    var convertedTime = new Date(epochTime);
    return convertedTime.getDate()+' '+monthNames[convertedTime.getMonth()]+' '+convertedTime.getFullYear();
    }

It can be used as getFormattedDate(1474356766254) and it returns a date string formatted as 20 September 2016.

This content has been helpful to you?

Thanks for contributing!

Yes No