0

If I set default moment time zone with moment.tz.setDefault(), is there a way to later retrieve the current default time zone?

Example:

const moment = require('moment-timezone');

moment.tz.setDefault('Asia/Tokyo');

console.log(moment.tz.getDefault()); // TypeError: moment.tz.getDefault is not a function
console.log(moment.tz.default);      // undefined
console.log(moment.tz.guess());      // America/New_York (my local timezone)
console.log(moment.tz.guess(true));  // America/New_York

// => I want something that will return "Asia/Tokyo"

This is different from the suggested duplicate because I don't want the browser's time zone, I specifically want to know what (if anything) moment.tz.setDefault() was set to.

Kip
  • 107,154
  • 87
  • 232
  • 265
  • Hello, Can't find anything online that allows you to do that but since you are pre-setting it in the setDefault wouldn't you be able to use some sort of if statement and and then do what you want to do? momentjs is also not maintained aaik, so I'd suggest dayjs, datefns or luxon – ImDarkk Feb 04 '22 at 20:22
  • @ImDarkk i'm dealing with a big legacy codebase, the call to setDefault() might happen somewhere out of my control – Kip Feb 04 '22 at 20:26
  • Does this answer your question? [Getting the client's time zone (and offset) in JavaScript](https://stackoverflow.com/questions/1091372/getting-the-clients-time-zone-and-offset-in-javascript) – Heretic Monkey Feb 04 '22 at 20:44
  • @HereticMonkey my question shows what `.guess()` does (which is not what I need) – Kip Feb 04 '22 at 20:52

1 Answers1

2

You can achieve that by moment.defaultZone.name:

const moment = require('moment-timezone');

moment.tz.setDefault('Asia/Tokyo');

console.log(moment.defaultZone.name)

Output:

Asia/Tokyo

Update after @Kip comment

  • If no default time zone is set, moment.defaultZone is undefined, so use moment.defaultZone?.name to be safe.
  • Other approach which uses documented functionality is moment().format('ZZ') which returns "+0900" in Tokyo time.
Danil Perestoronin
  • 1,063
  • 1
  • 7
  • 9
  • @HereticMonkey `tz.guess()` guesses the browser (or server, in node.js) time zone. The question is about retrieving the current moment default time zone, which `tz.guess()` ignores (or maybe it eventually falls back to it if it fails to come up with a guess?) – Kip Feb 04 '22 at 20:56
  • Yeah, I got that from your other comment. I'm usually very cautious about using undocumented properties in production code, but considering moment is no longer maintained, I guess you don't have to worry about that... – Heretic Monkey Feb 04 '22 at 21:02
  • 1
    Thanks, this seems to work. A few things to note- if no default time zone is set, `moment.defaultZone` is undefined, so I need to do `moment.defaultZone?.name` to be safe. Also, one other approach I found which uses documented functionality is `moment().format('ZZ')` which returns `"+0900"` in Tokyo time. That was enough for my use case (I really just wanted to know if it was set to anything other than UTC). – Kip Feb 04 '22 at 21:12
  • Thanks for the additions @Kip! I added it to the answer – Danil Perestoronin Feb 05 '22 at 13:23