2

I'm trying to get abbreviations of timezones with Moment.js.

still i am not getting the output

Moment.js provides https://momentjs.com/timezone/docs/#/using-timezones/getting-country-zones/

moment.tz.zonesForCountry('US')

which gives list of array( timezones of USA)

["America/Adak", "America/Anchorage", "America/Boise", "America/Chicago", "America/Denver", "America/Detroit", "America/Indiana/Indianapolis", "America/Indiana/Knox", "America/Indiana/Marengo", "America/Indiana/Petersburg", "America/Indiana/Tell_City", "America/Indiana/Vevay", "America/Indiana/Vincennes", "America/Indiana/Winamac", "America/Juneau", "America/Kentucky/Louisville", "America/Kentucky/Monticello", "America/Los_Angeles", "America/Menominee", "America/Metlakatla", "America/New_York", "America/Nome", "America/North_Dakota/Beulah", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", "America/Phoenix", "America/Sitka", "America/Yakutat", "Pacific/Honolulu"]

For abbreviations which i tried

let abbr = moment.tz.zonesForCountry('US').map((z) =>moment.tz(z).zoneAbbr());

output/required output

["HDT", "AKDT", "MDT", "CDT", "MDT", "EDT", "EDT", "CDT", "EDT", "EDT", "CDT", "EDT", "EDT", "EDT", "AKDT", "EDT", "EDT", "PDT", "CDT", "AKDT", "EDT", "AKDT", "CDT", "CDT", "CDT", "MST", "AKDT", "AKDT", "HST"]

tried using

 mtz.tz.zonesOfCountry('US').map((listzones) =>mtz.tz(listzones).zoneAbbr())

getting "undefined"

If you have any suggestion let me know.

user13465472
  • 107
  • 9
  • 1
    What is the outcome you are looking for? using ```let abbr = moment.tz.zonesForCountry('US').map((z) =>moment.tz(z).zoneAbbr());``` returns ```["HDT", "AKDT", "MDT", "CDT", "MDT", "EDT", "EDT", "CDT", "EDT", "EDT", "CDT", "EDT", "EDT", "EDT", "AKDT", "EDT", "EDT", "PDT", "CDT", "AKDT", "EDT", "AKDT", "CDT", "CDT", "CDT", "MST", "AKDT", "AKDT", "HST"]``` which seems like the desired behavior – Grant Singleton May 28 '20 at 16:53
  • What is the issue here exactly? – palaѕн May 28 '20 at 16:53
  • @GrantSingleton required output ["HDT", "AKDT", "MDT", "CDT", "MDT", "EDT", "EDT", "CDT", "EDT", "EDT", "CDT", "EDT", "EDT", "EDT", "AKDT", "EDT", "EDT", "PDT", "CDT", "AKDT", "EDT", "AKDT", "CDT", "CDT", "CDT", "MST", "AKDT", "AKDT", "HST"] i am not getting after using let abbr = moment.tz.zonesForCountry('US').map((z) =>moment.tz(z).zoneAbbr()) – user13465472 May 28 '20 at 17:27
  • this code is not working for me , do we any solution to get abbreviations – user13465472 May 28 '20 at 17:30
  • 1
    I ran it on my end and it works as expected. What error or output are you getting when logging ```abbr``` to the console? – Grant Singleton May 28 '20 at 17:33
  • it is showing undefined....actually i tried like this as well mtz.tz.zonesOfCountry('US').map((listzones) =>mtz.tz(listzones).zoneAbbr()) – user13465472 May 28 '20 at 17:49
  • What is ```mtz``` here? did you import moment as mtz? – Grant Singleton May 28 '20 at 18:09
  • Yes I have imported and I am using for other scenarios as well – user13465472 May 28 '20 at 18:18
  • I am sorry I wish I could help more. The code you posted works fine for me. There must be something wrong elsewhere. – Grant Singleton May 28 '20 at 18:41
  • Thanks for you reply ,do we have any other method to get abbreviations?? – user13465472 May 28 '20 at 18:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214848/discussion-between-grant-singleton-and-user13465472). – Grant Singleton May 28 '20 at 18:45

1 Answers1

0

var moment = require('moment-timezone'); moment().tz("America/Los_Angeles").format();

spatak
  • 1,039
  • 1
  • 14
  • 25
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Dec 26 '21 at 07:59