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

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

    What you want to achieve can be accomplished with native JavaScript. The object Date has methods that generate exactly the output you wish.
    Here are code examples:

    var d = new Date();
    console.log(d);
    >>> Sun Jan 28 2018 08:28:04 GMT+0000 (GMT)
    console.log(d.toLocaleDateString());
    >>> 1/28/2018
    console.log(d.toLocaleString());
    >>> 1/28/2018, 8:28:04 AM
    

    There is really no need to reinvent the wheel.

提交回复
热议问题