日期格式转换总结

10 篇文章 0 订阅

 

UTC:时间标准时间,世界标准时间

GMT:格林尼治时间

GST:北京时间

1、GMT时间格式化 yyyy-MM-dd HH:mm:ss

// Tue Jan 08 2019 15:41:36 GMT+0800 (中国标准时间) 解析出 yyyy-MM-dd HH:mm:ss
static dateToFormatString(date: Date) {
  const month = this.addZero(date.getMonth() + 1);
  const d = this.addZero(date.getDate());
  const h = this.addZero(date.getHours());
  const m = this.addZero(date.getMinutes());
  const s = this.addZero(date.getSeconds());


  return date.getFullYear() + '-' + month + '-' + d + ' ' +
    h + ':' + m + ':' + s;
}

static addZero(num) {
  if (num < 10) {
    return '0' + num;
  }
  return '' + num;
}

2、UTC时间格式化 yyyy-MM-dd 

// 2019-01-18T10:21:55.625 => 2019-01-18
static formatStringT(dateString: string) {
  if (dateString && dateString.includes('T')) return dateString.substr(0, dateString.indexOf('T'));
  else return '';
}

3、UTC时间格式化 yyyy-MM-dd HH:mm 

// 2019-01-18T10:21:55.625 => 2019-01-18 10:21
static formatStringThm(dateString: string) {
  if (dateString) {
    return dateString.substring(0, dateString.lastIndexOf(':')).replace('T', ' ');
  } else {
    return '--';
  }
}

4、UTC时间格式化  HH:mm:ss


// 2019-01-18T10:21:55.625 格式时间  获取时分秒
static getHms(dateString: string) {
  const d = new Date(dateString);
  return d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
}

5、时间戳转换成标准时间格式  yyyy-MM-dd HH:mm:ss

//stamp  时间戳 long
const date=new Date(stamp);
// 转换成yyyy-MM-dd HH:mm:ss
const str=date.getFullYear()+"-"+(date.getMonth()+1)+"-"
+date.getDate()+" "+date.getHours()+":"+date.getMinutes();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值