日期格式转换总结

it2022-05-05  168

 

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)