Search code examples
javascriptdatetimetimezonemomentjsmoment-timezone

How do I get the unique timezone name out of a moment.tz object?


Say you have a moment object, initialized to some timezone, say "US/Pacific" (not necessarily the browser's time zone). How do you get the "US/Pacific" name back out? I can get the abbreviation or the offset, but how do I get the actual unique timezone?

var m = moment.tz("US/Pacific");
console.log(m.zoneAbbr());  //  "PDT"
console.log(m.zoneName());  //  "PDT"
console.log(m.format("Z"));  //  "-07:00"
console.log(m.format("ZZ"));  //  "-0700"
console.log(m.format("z"));  //  "PDT"
console.log(m.format("zz"));  //  "PDT"

I want to get "US/Pacific" back out, as that's the globally unique name, regardless of offset or daylight savings, or anything.


Solution

  • You can get the name of the time zone that was assigned to the moment instance using the tz() method without any arguments.

    const dt = moment().tz('US/Pacific');
    console.log(dt.tz());
    // US/Pacific
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.js"></script>