时间戳转换为年月日时间

var formatTime = function(number,format) { //转换日期
            var formateArr  = ['Y','M','D','h','m','s'];
            var returnArr   = [];
            var date = new Date(number * 1000);
            returnArr.push(date.getFullYear());
            returnArr.push(formatNumber(date.getMonth() + 1));
            returnArr.push(formatNumber(date.getDate()));
            returnArr.push(formatNumber(date.getHours()));
            returnArr.push(formatNumber(date.getMinutes()));
            returnArr.push(formatNumber(date.getSeconds()));
            for (var i in returnArr)
            {
                format = format.replace(formateArr[i], returnArr[i]);
            }
            return format;
        }
        //数据转化
        var formatNumber = function(n) {
            n = n.toString();
            return n[1] ? n : '0' + n
        }

     
        console.log(formatTime(1453564799,'Y年M月D日')); //2016年01月23日
        console.log(formatTime(1488481383,'Y/M/D h:m:s')); //2017/03/03 03:03:03
 

 

上一篇:Leetcode 1305. All Elements in Two Binary Search Trees [Python]


下一篇:js 记录一次日期转换