moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.82k stars 835 forks source link

How does moment.tz(unixTimestamp,timezone) and moment.tz.guess() work? #1088

Closed Chakit22 closed 7 months ago

Chakit22 commented 8 months ago

I was trying to build a feature for a project where I had to use moment to convert a unixTimestamp to another timeZone's time. I tried to look through the documentation of moment-timezone and found this method moment.tz() which can work.

The issue is I am not finding anywhere in the documentation more about this function. I have some questions about it:

  1. What type of parameters does it except. Does it accept a string or a number?
  2. What kind of exceptions does it raise if I enter another type of parameter?
  3. I have also used moment.tz.guess(true) to detect the current timezone and I want to understand what kind of exceptions would moment.tz(unixTimestamp,timezone) raise if there is no timezone provided? The thing is it doesn't crash if there is no timezone provided too and it takes some other parameter? There is nothing mentioned in the docs from where does it fetch the timezone from if moment.tz.guess() returns undefined or null?

From the documentation i could just see that this is the function which can help me calculate the timezones but nothing more mentioned about the return types, parameters, errors, exceptions etc.

It'd be great if someone could get me answer's to these or some link to some other doc on the web which explains in detail?

Note: This is the doc I am referring: https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/

Thanks !

gilmoreorless commented 7 months ago

moment.tz() is just an extension to the base moment() function. The documentation you linked to explicitly says it takes all the parameters of moment(), with the addition of a String parameter at the end.

The return types and validations are all the same as moment() — you should read the core documentation to get more details on those. The only additions are: