1.将小于9的字符串前缀补零

参数:n

参数类型必填说明
nNumbern:0-9数字
const formatNumber = n => {
  n = n.toString();
  return n[1] ? n : '0' + n;
}

2.将时间戳格式化年月日时分秒

功能:YYYY-MM-DD hh:mm:ss

参数:date

参数类型必填说明
dateObject日期对象
const formatTimeYMDhms = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}

3.将时间戳格式化年月日

功能:YYYY-MM-DD

方法:formatTimeYMD(date)

参数:date

参数类型必填说明
dateObject日期对象
const formatTimeYMD = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return [year, month, day].map(formatNumber).join('-');
}

4.生成一段时间

功能:生成一组num天到当天字符串日期

方法:createTimeDate(num)

参数:date

参数类型必填说明
dateObject日期对象
const createTimeDate = num => {
  let arrDay = [];
  for(let i=0;i<num;i++){
    let tmpDay = new Date().setDate(new Date().getDate() - i);
    let str = timeUtil.formatTimeYMD(new Date(tmpDay))
    arrDay.push(str);
  }
  return arrDay;
}

5.将不存在时间段数据补零

方法:mergeTimeDate(arr ,num); 返回:返回合并后的时间段

参数:arr num

参数类型必填说明
arrArray日期数组
numNumber天数
const mergeTimeDate = (arr ,num) => {
  let brr = createTimeDate(num);
  let crr = brr.map((val) => {
    let tmpObj = {};
    tmpObj.date = val;
    tmpObj.count = 0;
    arr.forEach((v)=>{
      if(val == v.date){
        tmpObj.count = v.count;
      }
    });
    return tmpObj;
  });
  return crr;
}

6.封装成工具

//在utils文件加下建一个 time.js
export class TimeUtil{
  constructor(dat){
    if('' == dat || !dat) return '';
    this.$date = this.convert(dat);
    this.year = this.$date.getFullYear();
    this.month = this.$date.getMonth() + 1;
    this.day = this.$date.getDate();
    this.hour = this.$date.getHours();
    this.minute = this.$date.getMinutes();
    this.second = this.$date.getSeconds();
  }
  //将参数转成date对象
  convert(par){
    if('' == par) return ;
    let type = typeof par;
    switch (type){
      case 'object':return par;break;
      case 'number':return new Date(par);break;
      case 'string': let tmp = par.replace(/\-/g, "/");return  new Date(tmp);break;
    }
  }
  //将小于9的字符串前缀补零
  formatNumber(n){
    n = n.toString();
    return n[1] ? n : '0' + n;
  }
  //将时间对象转换 YYYY/MM/DD hh:mm:ss
  timeYMDHMS(){
    return [this.year,this.month,this.day].map(this.formatNumber).join('/') + ' ' + [this.hour, this.minute, this.second].map(this.formatNumber).join(':');
  }
  //将时间对象转换YYYY-MM-DD hh:mm:ss
  timeYMDHMS1(){
    return [this.year,this.month,this.day].map(this.formatNumber).join('-') + ' ' + [this.hour, this.minute, this.second].map(this.formatNumber).join(':');
  }
  //将时间对象转换YYYY.MM.DD
  timeYMD(){
    return [this.year, this.month, this.day].map(this.formatNumber).join('.');
  }
  //将时间对象转换YYYY-MM-DD
  timeYMD1(){
    return [this.year, this.month, this.day].map(this.formatNumber).join('-');
  }
  //将时间对象转换YYYY-MM-DD
  timeYMD2(){
    return [this.year, this.month, this.day].map(this.formatNumber).join('/');
  }
  //将时间对象转换 4月1日 ,昨天,今天,明天
  timeMD(){
    if(new Date().getDate() == (this.day + 1)){
      return '昨天';
    }else if(new Date().getDate() == this.day){
      return '今天';
    }else if(new Date().getDate() == (this.day - 1)){
      return '明天';
    }
    return this.month + '' + this.day + '';
  }
  //将时间对象转换 hh:mm:ss
  timeHMS(){
    return [this.hour, this.minute, this.second].map(this.formatNumber).join(':');
  }
}