js 时间是零区时间如何转成东八区时间

2023-10-05  本文已影响0人  lvyweb
//方法
function getStandardTime(date){
    let offset = 8;
    let targetTime = new Date(date);
    let utc = targetTime.getTime() + (-28800000);
    let actualTime = new Date(utc + (3600000 * offset));

    let year = actualTime.getFullYear();
    let month = (actualTime.getMonth() + 1) > 9 ? (actualTime.getMonth() + 1) : "0" + (actualTime.getMonth() +
        1);
    let day = actualTime.getDate() > 9 ? actualTime.getDate() : "0" + actualTime.getDate();
    let hour = actualTime.getHours() > 9 ? actualTime.getHours() : "0" + actualTime.getHours();
    let minute = actualTime.getMinutes() > 9 ? actualTime.getMinutes() : "0" + actualTime.getMinutes();
    let second = actualTime.getSeconds() > 9 ? actualTime.getSeconds() : "0" + actualTime.getSeconds();
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
//使用
getStandardTime("2023-10-06T06:30:20.334333+0000")
上一篇下一篇

猜你喜欢

热点阅读