Convert ISO Date to Date Format yyyy-mm-dd format in javascript

bPratikneeraj提出了一个问题:Convert ISO Date to Date Format yyyy-mm-dd format in javascript,或许与您遇到的问题类似。

回答者Mritunjay给出了该问题的处理方式:

Try this

date = new Date('2013-03-10T02:00:00Z');
date.getFullYear()+'-' + (date.getMonth()+1) + '-'+date.getDate();//prints expected format.

Update:-

As pointed out in comments, I am updating the answer to print leading zeros for date and month if needed.

date = new Date('2013-08-03T02:00:00Z');
year = date.getFullYear();
month = date.getMonth()+1;
dt = date.getDate();

if (dt < 10) {
  dt = '0' + dt;
}
if (month < 10) {
  month = '0' + month;
}

console.log(year+'-' + month + '-'+dt);

希望本文对你有帮助,欢迎支持JavaScript中文网