中国标准时间、2021-01-11T09:49:43.000+0000等各种时间的转换

1.将中国标准时间转换成yyyy-xx-nn aa-bb-cc

中国标准时间:
Thu May 12 2019 08:00:00 GMT+0800 (中国标准时间)

var d = new Date('Thu May 12 2019 08:00:00 GMT+0800 (中国标准时间)'); 
d=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

2.将yyyy-xx-nn aa-bb-cc转换成中国标准时间

 var s=new Date(stime).getTime();
          var d=new Date(etime).getTime();

3.如果遇到解析毫秒时间的时候,出现2020-7-8 11-7 可以使用分开解析

 let d = new Date(item[0].create_time);//这个是你需要解析的时间
let year = d.getFullYear().toString();
 let month = (d.getMonth()+1).toString();
 let day = (d.getDate()).toString();
 let hour = (d.getHours()).toString();
 let min = (d.getMinutes()).toString();
 let second = (d.getSeconds()).toString();
 if (year.length<=1){
     year='0'+year;
 }
 if (month.length<=1){
     month='0'+month;
 }
 if (day.length<=1){
     day='0'+day;
 }
 if (hour.length<=1){
     hour='0'+hour;
 }
 if (min.length<=1){
     min='0'+min;
 }
 if (second.length<=1){
     second='0'+second;
 }
 d = year+'-'+month+'-'+day+' '+hour+':'+min+':'+second;

4.标准时间转换成毫秒数
demo:
将Sat Nov 07 2020 16:52:13 GMT+0800 (中国标准时间)转成1604739133000
1.将转换的方法封装出来

 dateToMs (date) {
                let result = new Date(date).getTime();
                return result;
            },

2.调用该方法

console.log(this.dateToMs(demoTime))

5.将毫秒数转换成yyyy-xx-mm

1.关于时间转换的两个方法

 getMyDate(str) {
                let oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth() + 1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear + '-' + this.addZero(oMonth) + '-' + this.addZero(oDay) + ' ' + this.addZero(oHour) + ':' +
                        this.addZero(oMin) + ':' + this.addZero(oSen);
                return oTime;
            },
   addZero(num) {
       if (parseInt(num) < 10) {
           num = '0' + num;
       }
       return num;
   },

2.调用该方法就可以转换成 2020-11-07 18:55:13

6、2021-01-11T09:49:43.000+0000转yyyy-mm-mm aa-bb-cc

var data = 2021-01-11T09:49:43.000+0000.substr(0, 19);
var newDate = new Date(new Date(data.replace(/T/g, ' ').replace(/-/g, '/')).getTime() + 8 * 60 * 60 * 1000);
var d = new Date(newDate);
d=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
console.log(d)

你可能感兴趣的:(WEB前端开发,vue.js,javascript)