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

如何格式化和转换后台给的时间戳(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;
  }

转载:https://www.jianshu.com/p/4bf4650b98f7

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值