如何格式化和转换后台给的时间戳(2020-02-22T16:00:00.000Z)为标准的时间格式

背景:

把后台返回的时间格式,进行格式化,如:2020-02-22T16:00:00.000Z 转换格式为: 2020-02-22 16:00:00

方法一:
  formateDate(date) {
    const arr = date.split('T');
    const d = arr[0];
    const darr = d.split('-');

    const t = arr[1];
    const tarr = t.split('.000');
    const marr = tarr[0].split(':');

    const dd =
      parseInt(darr[0]) +
      '-' +
      parseInt(darr[1]) +
      '-' +
      parseInt(darr[2]) +
      ' ' +
      parseInt(marr[0]) +
      ':' +
      parseInt(marr[1]) +
      ':' +
      parseInt(marr[2]);
    return dd;
  }
方法二:
  // 日期数据 格式化 (公共函数)+ 数字补0操作
  addZero(num: any) {
    return num < 10 ? '0' + num : num;
  }
  formatDateTime(date: any) {
    const time = new Date(Date.parse(date));
    time.setTime(time.setHours(time.getHours() + 8));
    const Y = time.getFullYear() + '.';
    const M = this.addZero(time.getMonth() + 1) + '.';
    const D = this.addZero(time.getDate()) + ' ';
    const h = this.addZero(time.getHours()) + ':';
    const m = this.addZero(time.getMinutes()) + ':';
    // const s = this.addZero(time.getSeconds());
    return Y + M + D + h + m;
  }

你可能感兴趣的:(如何格式化和转换后台给的时间戳(2020-02-22T16:00:00.000Z)为标准的时间格式)