js时间戳转日期

时间:2021-01-13 02:34:06
    //时间戳转日期  2017-04-30 13:20
    //type=1--> 2017-04-30 13:20
    //type=2-->2018年08月
    //type=3-->2018-08-30
    function time_conversion(stamp,type){
        var date = new Date(stamp*1000),
            year = date.getFullYear(),
            month = date.getMonth() + 1 <10? '0' + (date.getMonth()+1) : date.getMonth()+1,
            dat = date.getDate() < 10 ? '0'+date.getDate() : date.getDate(),
            day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()],
            hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
            minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
            second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        var currentTime = year + "-" + month + "-" + dat + " " + hour + ":" + minute;
        var currentTime2 = year + "年" + month + "月";
        var currentTime3 = year + "-" + month + "-" + dat;
        if(type == 1){
            return currentTime;
        }

        if(type == 2){
            return currentTime2;
        }

        if(type == 3){
            return currentTime3;
        }
    }
   调用方法:console.log(time_conversion('1535595014',4));