JS 不同時間格式轉換(ISO時間&時間戳轉北京時間)

 //格林尼治2019-03-19T16:00:00.000Z  ==>>  2019-03-20 00:00:00   與北京時間8小時時差
            //格林尼治2019-03-19T16:00:00.000Z  ==>>  2019-03-20 00:00:00 
	       // //時間戳1553547600000  轉 //  2019-03-25T21:00:00.000Z
            function formDate(dateForm) {
                if (dateForm === "") {  //解決deteForm爲空傳1970-01-01 00:00:00
                    return "";
                }else{
                    var dateee = new Date(dateForm ).toJSON();
                    var date = new Date(+new Date(dateee)+ 8 * 3600 * 1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
                    return date;
                }
            }
            console.log(formDate("2019-03-19T16:00:00.000Z"))//2019-03-20 00:00:00
            console.log(formDate(1553547600000))             //2019-03-26 05:00:00
            console.log(formatDateT(1553547600000))          //2019-03-25T21:00:00.000Z

             //時間戳1553547600000  轉 //  2019-03-25T21:00:00.000Z
            function formatDateT(dataTime){
                // var timestamp3 = 1551686227000; 
                var timestamp = dataTime;
                var newDate = new Date(dataTime ); 
                // var newDate = new Date(dataTime + 8 * 3600 * 1000 );               

                newDate.getTime(timestamp * 1000);                
                // console.log(newDate.toDateString());//Mon Mar 11 2019                
                // console.log(newDate.toGMTString()); //Mon, 11 Mar 2019 06:55:07 GMT                
                // console.log(newDate.toISOString()); //2019-03-11T06:55:07.622Z
                return newDate.toISOString()
            }

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章