-1

I need to convert Mon Mar 07 2016 00:00:00 GMT-0500 (Eastern Standard Time) to `MM/DD/YYYY' format in javascript.

How can I do that?

gene
  • 2,098
  • 7
  • 40
  • 98
  • Have you looked through the questions in that "**Related**" list on the right side of this page? – Pointy Aug 22 '16 at 15:41
  • export function convertDate(inputFormat) { if (inputFormat.toString().length > 20) { function pad(s) { return s < 10 ? '0' + s : s; } const d = new Date(inputFormat); return [pad(d.getMonth() + 1), pad(d.getDate()), d.getFullYear()].join('/'); } return inputFormat; } – leogoesger Oct 25 '17 at 16:40

2 Answers2

3

Would something like this work for you? This is Maerics function literally the first post when I typed "Javascript change date format" the only difference is creating a new date out of the string and passing it into the function.

var tmpDate = new Date("Mon Mar 07 2016 00:00:00 GMT-0500 (Eastern Standard Time)")

function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat);
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
}

var shortDate = convertDate(tmpDate);

console.log(shortDate);
Community
  • 1
  • 1
Jonathan Newton
  • 832
  • 6
  • 18
1

what about getting a date like this:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
document.write(today);
fernando
  • 814
  • 1
  • 9
  • 24