How do I get a date in YYYY-MM-DD format?

前端 未结 10 841
一生所求
一生所求 2020-12-17 17:16

Normally if I wanted to get the date I could just do something like

var d = new Date(); console.log(d);

The problem with doing that, is when I

10条回答
  •  独厮守ぢ
    2020-12-17 17:49

    If you are trying to get the 'local-ISO' date string. Try the code below.

    function (date) {
        return new Date(+date - date.getTimezoneOffset() * 60 * 1000).toISOString().split(/[TZ]/).slice(0, 2).join(' ');
    }
    

    +date Get milliseconds from a date.

    Ref: Date.prototype.getTimezoneOffset Have fun with it :)

提交回复
热议问题