js 时间戳转年月日

var t=1521694261;

    console.log(timestampToTime(t))

    timestampToTime(t)

    function timestampToTime(timestamp) {

        var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000

    let Y = date.getFullYear() + '-';

    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';

    let D = change(date.getDate()) + ' ';

    let h = change(date.getHours()) + ':';

    let m = change(date.getMinutes()) + ':';

    let s = change(date.getSeconds());

        return Y + M + D + h + m + s;

    }

    function change(t) {

      if (t < 10) {

         return "0" + t;

       } else {

         return t;

       }

    }

js 时间戳转年月日