js Unix时间戳转化时间字符串

function str_date(udate,format="yyyy-MM-dd h:m:s") {
        
        if (udate) {
            udate=parseInt(udate);
            var newDate = new Date(udate)
        } else {
            var newDate = new Date()
        }
        var date = {
           "M+": newDate.getMonth() + 1,
           "d+": newDate.getDate(),
           "h+": newDate.getHours(),
           "m+": newDate.getMinutes(),
           "s+": newDate.getSeconds(),
           "q+": Math.floor((newDate.getMonth() + 3) / 3),
           "S+": newDate.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
           format = format.replace(RegExp.$1, (newDate.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
           if (new RegExp("(" + k + ")").test(format)) {
               format = format.replace(RegExp.$1, RegExp.$1.length == 1
                  ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
           }
        }
        return format;
    }
    console.log(str_date('1508062530000'));

你可能感兴趣的:(js Unix时间戳转化时间字符串)