2

I want to set current time zone with moment.js for user that runs my app. My problem is that many of momentjs question posted here have method called tz but I can't see it now in 2020.

Format date in a specific timezone

With that method you can set this:

moment().tz('America/Phoeinx')

But I don't want to set this manually. I found utc method but it returns me timezone +0000.

How to set current time zone with momentjs in 2020?

Freestyle09
  • 4,894
  • 8
  • 52
  • 83
  • `Date.prototype.getTimezoneOffset()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset – Ivar Nov 10 '20 at 09:24
  • 1
    Does this answer your question? [Get timezone from users browser using moment(timezone).js](https://stackoverflow.com/questions/40401543/get-timezone-from-users-browser-using-momenttimezone-js) – Ivar Nov 10 '20 at 09:24
  • You could try [`moment.tz.guess()`](https://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/) – wgumenyuk Nov 10 '20 at 09:30
  • @Aivaras I need to use momentjs not Date – Freestyle09 Nov 10 '20 at 09:31
  • @wgumenyuk tz is no longer available – Freestyle09 Nov 10 '20 at 09:31

1 Answers1

2

Ok I found an answer, all that we need is just get current utcOffset from momentjs library:

const currentUtcOffset = moment().utcOffset()

and pass it to utc method

moment(date).utc(currentUtcOffset).toISOString()

Now everything works perfect.

Freestyle09
  • 4,894
  • 8
  • 52
  • 83