简体   繁体   中英

Stringifying date causes timezone change

I am stringifying an object which contains moment object. It changes the timezone. How do I preserve the time zone even after stringifying the object.

localStorage.setItem("energy_meter", JSON.stringify(localStorageObj));

localStorageObj.ltg.push({
            ltgId: message["topic"].split('/')[1],
            time: (moment()),
            message: message.payloadString,
            floorNo: message["topic"].split('/')[2] || null
        })

That is the object I am stringifying.

date = new Date();
let hours = date.getHours() - date.getTimezoneOffset() / 60;
let minutes = (date.getHours() - date.getTimezoneOffset()) % 60;
date.setHours(hours);
date.setMinutes(minutes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM