JavaScript Date对象转时间

Daming 2017-10-12
0条评论 664 次浏览
Daming 2017-10-120条评论 664 次浏览
  Vue.prototype.showTime = function (d) {
    let year;
    let month;
    let date;
    let hour;
    let minute;
    let second;
    let nowtime;
    if(d){
       nowtime = new Date(d);
    }else {
      nowtime = new Date();//没有传入date 就按现在的时间
    }

    year = nowtime.getFullYear();
    month = nowtime.getMonth() + 1;
    date = nowtime.getDate();
    hour = nowtime.getHours();
    if (hour < 10) {
      hour = "0" + hour;
    }
    minute = nowtime.getMinutes();
    if (minute < 10) {
      minute = "0" + minute;
    }
    second = nowtime.getSeconds();
    if (second < 10) {
      second = "0" + second;
    }

    let nowTime = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    return nowTime;
  };
0

发表评论

电子邮件地址不会被公开。