简体   繁体   中英

How to convert the local browser time to utc time unsing moment.js

Tried multiple methods to convert the local time to UTC. Tried Offset(420) as the time difference is UTC 7 and it worked. So here what i need is my users will be giving a request from different timezones and all of them should convert to UTC. So when giving time from different timezone the UTC time difference will also change how it can be identified and change accordingly. how can i do that?

You can use moment.utc() to get UTC time. If you wish to send it to the server, it's probably best to send as an ISO-8601 date string.

 const localDate = moment(); const utcDate = moment.utc(); console.log("Local time:", localDate.format("YYYY-MM-DD HH:mm:ss")); console.log("UTC time:", utcDate.format("YYYY-MM-DD HH:mm:ss"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

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