-2

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.

Asons
  • 84,923
  • 12
  • 110
  • 165
  • 1
    _"I tried below links"_ doesn't say much, instead provide a [mcve] so we can see the issue. – Asons Aug 12 '19 at 19:55
  • And if this should be done client side, don't add `asp.*` tags to the tags list. – Asons Aug 12 '19 at 19:57
  • 1
    No misuse of power here, I simply explained what is expected of a poster, and if anything is unclear, reread [ask]. Also, the tags list is not for you to target a given user group, it is to tell the users what the question is about. Users can subscribe to a tag, and the one's who wants `asp` do not want `javascript` at the same time (unless they subscribed to both) – Asons Aug 12 '19 at 20:04
  • You've been here long enough now, to know how to post a proper question. – Asons Aug 12 '19 at 20:17
  • 1
    Realize that time conversion cannot be divorced completely from date. For instance, on March 10th, 2019 at 01:59:59.999 AM here on the east coast of the United States, we did not proceed to 02:00:00.000 AM in the next millisecond. Instead we transitioned directly to 03:00:00.000 AM. If you were tracking an event in UTC, however, it would still have recorded 06:00:00.000 AM, since UTC does not track DST. – Heretic Monkey Aug 12 '19 at 20:24

1 Answers1

1

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.

Asons
  • 84,923
  • 12
  • 110
  • 165
  • 1
    Note that this may or may not give you the correct time, depending on the time recorded and what date it was recorded on, due to the vagaries of Daylight Saving Time. Also, whenever you parse dates from strings in Moment, you should specify a date format. See [the Parsing Guide in the docs](https://momentjs.com/guides/#/parsing/). – Heretic Monkey Aug 12 '19 at 20:27
  • @HereticMonkey That it's true. It is better to use a UNIX timestamp instead of time value to store the time in the database. But given a time value like that, you cannot take in the account the daylight saving time. Regarding the moment parsing, in the docs, it says that you should specify a format only when you use anything other than an ISO 8601 format [https://momentjs.com/docs/#/parsing/string/ – Laurentiu Turcu Aug 12 '19 at 20:38