-1

I've a JS function which is parsing datetime. This is the output I get : 2017-08-15. I need to display like this 8/15/2017 12:45 PM. How can I do it?

Current JavaScript Function :

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2)
        month = '0' + month;
    if (day.length < 2)
        day = '0' + day;
    return [year, month, day].join('-');
}
Partha
  • 81
  • 1
  • 10

1 Answers1

0

It might be this line return [year, month, day].join('-');

You might want to rephrase that to be return [month, day, year].join('/');

  • Nice Thanks. That will change the format to 8/15/2017. But I also want the mm:ss AM/PM. Any quick help? – Partha May 03 '21 at 18:39