重点:用时间戳进行加减运算

//转为时间戳格式 
let dateTime = new Date().getTime();
//进行加减运算 
dateTime = dateTime + 1000*60*30; //向后30min
dateTime = dateTime + 1000*60*60; // 向前1h
//最后再把得到的时间戳转回来
      //转回指定日期格式
      dateTime = this.formatDate(new Date(dateTime));
 
    formatDate (date) {
      function deal (t) {
        return t < 10 ? "0" + t : t;
      }
      let y = date.getFullYear();
      let m = deal(date.getMonth() + 1);
      let d = deal(date.getDate());
      let h = deal(date.getHours());
      let M = deal(date.getMinutes());
      let s = deal(date.getSeconds());
      return `${y}-${m}-${d} ${h}:${M}:${s}`;
    },

 

posted on 2021-05-28 16:48  巍MG  阅读(221)  评论(0编辑  收藏  举报