javascript对象转为字符串

    function getStringTime(time){
         //年
      year = time.getFullYear();
      //月
      month = time.getMonth()
     
     if(String(month).length == 1){
         month = "0"+month
     }

      //日
      day = time.getDate();
      if(String(day).length==1){
          month= "0"+day;
      }

    //时
     hour = time.getHours();
     if(String(hour).length == 1){
         hour = "0"+hour;
     }
     //分
     minute = time.getMinutes()
     if(String(minute).length == 1){
         minute ="0"+minute;
     }
     //秒
     second = time.getSeconds();
     if(String(second).length ==1){
         second = "0"+second;
     }

    t = `${year}-${month}-${day} ${hour}:${minute}:${second}`;

    return t;

    }


   result =  getStringTime(time);

   console.log(result);
     

  

posted @ 2018-05-10 15:20  雷神约  阅读(300)  评论(0编辑  收藏  举报