How to get year/month/day from a date object?

前端 未结 16 1046
暗喜
暗喜 2020-11-28 17:52

alert(dateObj) gives Wed Dec 30 2009 00:00:00 GMT+0800

How to get date in format 2009/12/30?

16条回答
  •  清酒与你
    2020-11-28 18:28

    With the accepted answer, January 1st would be displayed like this: 2017/1/1.

    If you prefer 2017/01/01, you can use:

    var dt = new Date();
    var date = dt.getFullYear() + '/' + (((dt.getMonth() + 1) < 10) ? '0' : '') + (dt.getMonth() + 1) + '/' + ((dt.getDate() < 10) ? '0' : '') + dt.getDate();
    

提交回复
热议问题