Search code examples
momentjsmoment-timezone

Get code from country or zone name to set locale() moment.js


I'm using moment.js with moment timezone in it.

I was trying to find how to get code 'fr' from zone name 'Europe/Paris' all day, but with no luck.

I read all their docs and couldn't find that function either, so i need help on this one.

Closest what i found is this. I need this function but in reverse, to pass country name and get country code.


Solution

  • Luckily i found solution

    moment.tz.zone('America/New_York').countries()
    

    This will get you array so you guys can easily pass that value to locale()

    So final code would be:

    var get_locale = moment.tz.zone('America/New_York').countries();
    moment.locale(get_locale);