有关时间转换的一些方法

  //格式化时分秒
  chartTimeToSec(time) {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var dates = new Date();
    var sq = this.getClientTimezone(dates);
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var nowDate = year + "/" + month + "/" + day;
    var times = nowDate+' '+time;
    var oldTime = (new Date(times)).getTime()+sq*60*60*1000; //得到毫秒数
    return oldTime;
  },

    // 通过时区计算北京时间
    computeBeiJingDate(time){
    var date = new Date(time);
    var sq = this.getClientTimezone(date);
    if(sq == 8){
      return (date.getHours() < 10? "0" + date.getHours(): date.getHours()) + ":" + (date.getMinutes() < 10? 
"0" + date.getMinutes(): date.getMinutes()) + ":" + (date.getSeconds() < 10? "0" + date.getSeconds(): 
date.getSeconds());
    }
    var timestamp = date.getTime() + (8 - sq) * 60 * 60 * 1000;
    var dateAfter = new Date(timestamp);
    // return dateAfter.getFullYear() + "-" + (dateAfter.getMonth() + 1) + "-" + dateAfter.getDate() + " " + 
  dateAfter.getHours() + ":" + dateAfter.getMinutes() + ":" + dateAfter.getSeconds();
      return (dateAfter.getHours() < 10? "0" + dateAfter.getHours(): dateAfter.getHours()) + ":" + 
(dateAfter.getMinutes() < 10? "0" + dateAfter.getMinutes(): dateAfter.getMinutes()) + ":" + 
(dateAfter.getSeconds() < 10? "0" + dateAfter.getSeconds(): dateAfter.getSeconds());
    },

// 通过北京时间计算其他时区
  computeOtherDate(time){
    var date = '';
    if (!!window.ActiveXObject || "ActiveXObject" in window){
      date = new Date().getFullYear() + "/" + (new Date().getMonth() + 1) + "/" + new Date().getDate() + " " + time;
    }else{
      date = new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate() + " " + time;
    }
    var daten = new Date(date);
    var sq = this.getClientTimezone(daten);
    var hours = daten.getHours() - (8 - sq);
    if(hours < 0){
      // return daten.getFullYear() + "-" + (daten.getMonth() + 1) + "-" + (daten.getDate() - 1)  + " " + (24 + hours) + ":" + daten.getMinutes() + ":" + daten.getSeconds();
      return ((24 + hours) < 10? "0" + hours: hours) + ":" + (daten.getMinutes() < 10? "0" + daten.getMinutes(): daten.getMinutes()) + ":" + (daten.getSeconds() < 10? "0" + daten.getSeconds(): daten.getSeconds());
    }else{
      // return daten.getFullYear() + "-" + (daten.getMonth() + 1) + "-" + daten.getDate()  + " " + hours + ":" + daten.getMinutes() + ":" + daten.getSeconds();
      return (hours < 10? "0" + hours: hours) + ":" + (daten.getMinutes() < 10? "0" + daten.getMinutes(): daten.getMinutes()) + ":" + (daten.getSeconds() < 10? "0" + daten.getSeconds(): daten.getSeconds());
    }
  }

 //格式化时分秒
  transformTime(timestamp = +new Date()){
    if (timestamp) {
      var time = new Date(timestamp);
      var y = time.getFullYear(); //getFullYear方法以四位数字返回年份
      var M = time.getMonth() + 1; // getMonth方法从 Date 对象返回月份 (0 ~ 11),返回结果需要手动加一
      var d = time.getDate(); // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
      var h = time.getHours(); // getHours方法返回 Date 对象的小时 (0 ~ 23)
      var m = time.getMinutes(); // getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
      var s = time.getSeconds(); // getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
      return  this.addZero(h) + ':' + this.addZero(m) + ':' + this.addZero(s);
    } else {
      return '';
    }
  },
  //补零
  addZero(m){
    return m < 10 ? '0' + m : m;
  },

你可能感兴趣的:(有关时间转换的一些方法)