Format JavaScript date as yyyy-mm-dd

后端 未结 30 3188
再見小時候
再見小時候 2020-11-22 01:28

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using JavaScript?

30条回答
  •  悲哀的现实
    2020-11-22 02:06

    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);
    

提交回复
热议问题