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

前端 未结 16 1096
暗喜
暗喜 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:51

    I am using this which works if you pass it a date obj or js timestamp:

    getHumanReadableDate: function(date) {
        if (date instanceof Date) {
             return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
        } else if (isFinite(date)) {//timestamp
            var d = new Date();
            d.setTime(date);
            return this.getHumanReadableDate(d);
        }
    }
    

提交回复
热议问题