1、2020-12-02T02:13:38.376232Z
T表示分隔符,Z表示的是UTC。
UTC:世界标准时间,在世界标准时间上加上8小时,即东八区时间,也就是北京时间。
2、所以按照这个思路,先将uct时间转换为时间戳,然后加上8个小时,再格式化为想要的时间格式
let text = "2020-12-02T02:13:38.376232Z";
let json_date = new Date(text).toJSON();
let a = new Date(json_date).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
let b = new Date(a).getTime()+8 * 3600 * 1000; //时间戳,加上8个小时
let c = format(b);
console.log(c);
//格式化时间戳
let format = (shijianchuo) =>
{
//shijianchuo是整数,否则要parseInt转换
let time = new Date(shijianchuo);
let y = time.getFullYear();
let m = time.getMonth()+1;
let d = time.getDate();
let h = time.getHours();
let mm = time.getMinutes();
let s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
输出为
2020-12-02 10:13:38
3、后来经发现,根本不需要这么麻烦,new Date()即是转换为中国标准时间,然后直接格式化为想要的格式即可
let text = "";
//转换为中国标准时间(VM6363:1 Wed Dec 02 2020 10:13:38 GMT+0800 (中国标准时间))
let a = new Date(text);
//格式化
let b = format(a);
console.log(b);
//格式化时间
let format = (time) =>
{
let y = time.getFullYear();
let m = time.getMonth()+1;
let d = time.getDate();
let h = time.getHours();
let mm = time.getMinutes();
let s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
输出为
2020-12-02 10:13:38