I have a date with the format Sun May 11,2014
. How can I convert it to 2014-05-11
using JavaScript?
This worked for me to get the current date in the desired format (YYYYMMDD HH:MM:SS):
var d = new Date();
var date1 = d.getFullYear() + '' +
((d.getMonth()+1) < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1)) +
'' +
(d.getDate() < 10 ? "0" + d.getDate() : d.getDate());
var time1 = (d.getHours() < 10 ? "0" + d.getHours() : d.getHours()) +
':' +
(d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes()) +
':' +
(d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds());
print(date1+' '+time1);