Get String in YYYYMMDD format from JS date object?

后端 未结 30 2243
一个人的身影
一个人的身影 2020-11-22 10:47

I\'m trying to use JS to turn a date object into a string in YYYYMMDD format. Is there an easier way than concatenating Date.getYear()

30条回答
  •  遥遥无期
    2020-11-22 11:46

    Little bit simplified version for the most popular answer in this thread https://stackoverflow.com/a/3067896/5437379 :

    function toYYYYMMDD(d) {
        var yyyy = d.getFullYear().toString();
        var mm = (d.getMonth() + 101).toString().slice(-2);
        var dd = (d.getDate() + 100).toString().slice(-2);
        return yyyy + mm + dd;
    }
    

提交回复
热议问题