时间戳转换成年月日时分秒格式

   function getNowFormatDate() {
        var date = new Date();         //获取时间
        var year = date.getFullYear();  //年
        var month = date.getMonth() + 1; //月
        var strDate = date.getDate();   //日
        var hours = date.getHours();    //时
        var minutes = date.getMinutes(); //分
        var second = date.getSeconds(); //秒

        //判断时间的
        // if (month >= 1 && month <= 9) {
        //     month = "0" + month;
        // }
        // if (strDate >= 0 && strDate <= 9) {
        //     strDate = "0" + strDate;
        // }
        // if(hours >= 0 && hours <= 9){
        //     hours = "0" + hours;
        // }
        // if(minutes >= 0 && minutes <= 9){
        //     minutes = "0" + minutes;
        // }
        // if(second >= 0 && second <= 9){
        //     second = "0" + second;
        // }
        //三元表达式优化

        var currentdate = year +'-'+ 
        (month>9? month : '0'+month)  +'-'+ 
        (strDate>9? strDate : '0'+strDate) +' '+ 
        (hours>9? hours : '0'+hours) +':'+
        (minutes>9? minutes : '0'+minutes) +':'+ 
        (second>9? second : '0'+second) ;

        console.log(currentdate)
        return currentdate;
    }
    getNowFormatDate()

你可能感兴趣的:(时间戳转换成年月日时分秒格式)