首页 > 技术文章 > JS - 2020-01-01T00:00:00.000000Z 日期格式转换、Sun Jul 04 2021 00:00:00 GMT+0800 (中国标准时间)转2021-07-04 00:00

liazhimao 2020-10-16 15:26 原文

日期转换格式

2020-06-27T14:20:27.000000Z 时间格式转换成 2020-06-27 14:20:27

1 function rTime(date) {
2     var json_date = new Date(date).toJSON();
3     return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') 
4 }
5 let date = rTime('2020-06-27T14:20:27.000000Z');
6 console.log(date) // 2020-06-27 14:20:27

toJSON() 方法可以将 Date 对象转换为字符串,并格式化为 JSON 数据格式。

自己项目中的
 1 // 时间过滤
 2                     this.List[i].date = this.List[i].requestUserTime;
 3 
 4                     function rTime(date) {
 5                         var json_date = new Date(date).toJSON();
 6                         return new Date(+new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
 7                             '')
 8 
 9                     }
10 
11                     this.List[i].date1 = rTime(this.List[i].date);

 Sun Jul 04 2021 00:00:00 GMT+0800 (中国标准时间)转2021-07-04 00:00


timeFormat(time) {
      // 时间格式化 2019-09-08
      let year = time.getFullYear();
      let month = time.getMonth() + 1 > 10 ? time.getMonth()+1 : '0'+(time.getMonth()+1);
      let day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
      let h = time.getHours()>= 10 ? time.getHours() : "0" + time.getHours();              //获取当前小时数(0-23)
      let m = time.getMinutes()>= 10 ? time.getMinutes() : "0" + time.getMinutes();         //获取当前分钟数(0-59)
      console.log(year + "-" + month + "-" + day+ " " + h+ ":" + m)
      return year + "-" + month + "-" + day+ " " + h+ ":" + m;
      
    },

 

 

推荐阅读