2

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.

Stefan
  • 627
  • 6
  • 19

2 Answers2

3

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);
Stefan
  • 627
  • 6
  • 19
1

I was trying to below code in my Angular(v12.0.5) project and get this error.

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

Error -

moment-error

package.json -

"dependencies": {
    "@angular/animations": "^12.0.5",
    "@angular/cdk": "~12.2.13",
    "@angular/common": "~12.0.3",
    "@angular/compiler": "~12.0.3",
    "@angular/core": "~12.0.3",
    "@angular/forms": "~12.0.3",
    "@angular/material": "~12.2.13",
    "@angular/material-moment-adapter": "^13.2.2",
    "@angular/platform-browser": "~12.0.3",
    "@angular/platform-browser-dynamic": "~12.0.3",
    "@angular/router": "~12.0.3",
    "moment": "^2.29.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.1.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~12.0.3",
    "@angular/cli": "~12.0.3",
    "@angular/compiler-cli": "~12.0.3",
    "@types/jasmine": "~3.6.0",
    "@types/moment-timezone": "^0.5.30",
    "@types/node": "^12.11.1",
    "typescript": "~4.2.3"
  }

Then I found the solution in moment doc -

const get_country = moment.tz.guess(true); // Asia/Calcutta
const get_locale = moment.locale(get_country); // en
Pinaki
  • 792
  • 8
  • 17