2

I've tried searching for this but it's a pretty specific question. I have a React application where users will be looking at a calendar that can be switch between multiple timezones. I'm using Moment with it's Timezone plugin to try and standardize these times as much as possible.

There is a use case with this application where, for example, a user may be in the US/Eastern (UTC -05:00) timezone viewing a calendar that is set in the US/Mountain (UTC -07:00) timezone.

Right now, I'm keeping my dates in UTC to try and minimize the confusion, however I will need this user, in the Eastern timezone, to be able to see the date in the Mountain timezone. For instance, clicking on a calendar appointment, which is set in Mountain time, will display at 3pm MT. However, right now, when I take the UTC date, it's being converted into the user's local time. A 3pm MT appointment is displaying as 5pm ET.

// I'd like it to display 2017-12-20T15:00:00-07.00 to the end user
moment('2017-12-20T22:00:00Z').format() // returns 2017-12-20T17:00:00-05.00

My question is, using the calendar offset (UTC -07:00), how can I display that time in MT as opposed to the user's local time using Moment and Moment Timezone? I don't have access to any other timezone information, besides the offset. I'm tried going through Moment Timezone's docs but it seems I need a name to create the time in a timezone different from the user's local timezone.

addam
  • 757
  • 1
  • 6
  • 11
  • 1
    have you looked at the very bottom of the homepage for moment timezone? *Convert Dates Between Timezones* – Jaromanda X Dec 20 '17 at 13:01
  • I have, however I only have access to the offset values (-07:00) and not the timezone names. I can't find a way in the Moment Timezone documentation to connect the two to each other. – addam Dec 20 '17 at 13:04
  • Right, so -7:00 is just **one example**? you could have -9:00, -10:00 -6:00 etc etc? – Jaromanda X Dec 20 '17 at 13:09
  • Exactly, but the user's timezone could also be any variation of those offsets as well. When I try to take my UTC date and convert it to a time that is different than the user's timezone, it doesn't work. Moment is always returning the time in the user's local timezone. I see in the Moment Timezone documentation you can return a date in a different timezone format but it seems that you need a timezone name and not just the offset. – addam Dec 20 '17 at 13:18
  • maybe something in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat will help – Jaromanda X Dec 20 '17 at 13:27
  • After reading https://stackoverflow.com/tags/timezone/info - especially the _Time Zone != Offset_ section - it seems there isn't a solution to my question without another API call. Once I can get the timeZoneId using one of the APIs [listed here](https://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates), I can then use Moment Timezone to create my date. – addam Dec 20 '17 at 17:37

1 Answers1

-1

To use moment-timezone, you will need moment@2.9.0+, moment-timezone.js, and the moment-timezone data.

I think this will helps you.

https://momentjs.com/timezone/docs/

Ajay sharma
  • 41
  • 1
  • 8