简体   繁体   中英

Converting UTC Time Field To Browser Time Using Moment.js

I'm receiving ONLY UTC time from my db like 02:10:12, I want to convert this time to users browser time.

I tried below links

Moment Js UTC to Local Time Changing the Utc Date to Local Date using moment.js Convert UTC time to Local time of Browser

but none of them worked for me since most of them work with datetime field, mine is only time type in my database.

What I want to do is this,if my db UTC time is 02:10:12 then using moment.js we detect users browser timezone and convert this UTC time to that time zone and show in UI.

Well, moment works with date and time. What you can do is create a moment with the current date and the said time and then use the format method to display the time only.

moment.utc(`2019-08-19 ${server_utc_time}`).local().format('HH:mm:ss');

where server_utc_time is the utc time from your server.

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