Format JavaScript date as yyyy-mm-dd

后端 未结 30 2683
再見小時候
再見小時候 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:12

    function myYmd(D){
        var pad = function(num) {
            var s = '0' + num;
            return s.substr(s.length - 2);
        }
        var Result = D.getFullYear() + '-' + pad((D.getMonth() + 1)) + '-' + pad(D.getDate());
        return Result;
    }
    
    var datemilli = new Date('Sun May 11,2014');
    document.write(myYmd(datemilli));

提交回复
热议问题