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

前端 未结 10 800
一生所求
一生所求 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:48

    Just use the built-in .toISOString() method like so: toISOString().split('T')[0]. Simple, clean and all in a single line.

    var date = (new Date()).toISOString().split('T')[0];
    document.getElementById('date').innerHTML = date;

    Please note that the timezone of the formatted string is UTC rather than local time.

提交回复
热议问题