M1JBR M1JBR - 1 month ago 6
Javascript Question

How to convert PDT date to yyyymmdd in javascript

i have a PDT data in the following format

Tue Jul 14 02:00:00 PDT 2015


I tried to convert it to yyyymmdd format, but always i got NAN results,

how could i do it ?

Answer

We have no idea why you have problems. It could be browser or a typo

In any case here is the short version tested in Chrome and IE11

function pad(num) {
  return String("0"+num).slice(-2);
}
function formatDate(date) {
  return date.getFullYear()+pad(date.getMonth()+1)+date.getDate();
}
var date = new Date("Tue Jul 14 02:00:00 PDT 2015");
console.log(formatDate(date));