其他分享
首页 > 其他分享> > 时间戳转换为年月日时间

时间戳转换为年月日时间

作者:互联网

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
 

 

标签:03,转换,formatNumber,returnArr,var,时间,push,date,年月日
来源: https://blog.csdn.net/mo_jiu/article/details/99545572