Moment-Timezone is using default locale after global Moment locale is set

moment/locale is not a function
moment get local timezone
moment.tz is not a function
moment get timezone offset
angular-moment-timezone
import moment-timezone
moment-timezone map
moment js size

I'm building an application written in Typescript which uses features from both Moment.js and moment-timezone. I need the date and timestamps within the application localized so in the main app.ts file I set moment's locale using the language of the device.

UPDATE: here is a gist of the sample files with additional comments https://gist.github.com/spstratis/fa853f9750a095d4acd0d1196a285be9

app.ts

import * as moment from 'moment/min/moment-with-locales';

let language = appUtil.getPhoneLanguage();

moment.locale(language);

// the expected locale is printed
console.log("Moment Locale = " + moment.locale());

The issue is, in this utilities module when I import moment-timezone, it is defaulting to 'en' locale even though I have set moment's locale globally in the main app.ts file.

Below are two of my utility methods, how can I localize the relative date strings and months if moment-timezone is defaulting them to 'en'?

I tried adding the .locale(locale) to the moment methods but that didn't change anything. If I imported moment instead of moment-timezone that worked for some of the methods but failed on any of them which needed to use the timezone utilities.

date-util.ts

import * as moment from 'moment-timezone';

export function dateRelativeTime(value): string {
  let timezoneId = appCache.getTimezoneId();
  let localTime = _getLocalUtcDateString(value, timezoneId);
  let dateMoment = moment(localTime, "MM/DD/YYYY hh:mm:ss A");
  let formatedDate = dateMoment.tz(timezoneId).fromNow();

  return formatedDate;
};

export function localizedMonths(): ValueList {
  let m = moment("2016");
  let months = new ValueList([]);
  for (var i = 0; i < 12; i++) {
    months.push({ ValueMember: [i + 1], DisplayMember: m.month(i).format('MMMM') });
  }

  return months;
};

You are importing moment wrong. Don't do this:

import * as moment from 'moment/min/moment-with-locales';

Just do this:

import * as moment from 'moment';

It will load the individual locales as you use them (on Node.js) and you will then be sharing the same moment dependency that moment-timezone uses, so your global locale will carry through.

moment.locale(String) doesn't change locale globally · Issue #1875 , If you want to "remote control" moments locale after creation - we'll Upon app startup, I set up moment's locale globally, say to German: 'moment.locale('de') . the import/require iff your moment and moment-timezone are resolving to import "moment/locale/fr"; //always use French export default function  moment.tz.setDefault (String); By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node.js server. To change the default time zone, use moment.tz.setDefault with a valid time zone.


I had the same issue in Typescript. I wanted to use moment-timezone to set timezone to Europe/Brussels and set locale to Belgian Dutch and I solved it like this:

import 'moment/locale/nl-be';
import * as momentTZ from 'moment-timezone';
momentTZ.locale("nl-be");
momentTZ.tz.setDefault("Europe/Brussels");

Now use momentTZ if you want to use moment in your project, like this f.e.

const exampleDate = momentTZ().format('dddd D MMMM');

moment.tz.setDefault() is global and shared by all modules. · Issue , Default timezone is shared between all modules: server.js var console.log('sets timezone to Europe/Stockholm and locale to sv'); moment.tz.setDefault('Europe/​Stockholm'); moment.locale('sv'); var modu Output after 3 seconds: Wrong locales when using in different files rotaready/moment-range#  By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use. To load a locale, pass the key and the string values to moment.locale .


Have you tried .format('') ?

moment.locale();         // en
moment().format('LT');   // 6:27 PM
moment().format('LTS');  // 6:27:51 PM
moment().format('L');    // 05/31/2017
moment().format('l');    // 5/31/2017
moment().format('LL');   // May 31, 2017
moment().format('ll');   // May 31, 2017
moment().format('LLL');  // May 31, 2017 6:27 PM
moment().format('lll');  // May 31, 2017 6:27 PM
moment().format('LLLL'); // Wednesday, May 31, 2017 6:27 PM
moment().format('llll'); // Wed, May 31, 2017 6:27 PM

UPDATE: Also make sure your moment lib is the one with locale in it: https://momentjs.com/ specifies there are two moment libraries, with ( moment-with-locales.js ) and without locale ( moment.js ).

Moment Timezone, moment-timezone-with-data.js is recommended for server environments (Node.js​) Webpack documentation for an example of how to reduce Moment's bundled locale data. Уou may specify a boolean right after format argument to use strict parsing. to the moment instance or; undefined if a time zone has not been set. And for each moment instance to have its own local config. This would also be great for applications using Express, where you can then create a moment instance for the duration of the request, configure it with a global timezone, and rest assured that all the rest of your middleware will use the correct timezone.


Docs, Note: if you want to work with a particular variation of moment timezone, for example Note: In 2.4.0, the globally exported moment object was deprecated. Notable files are moment.js , locale/*.js and min/moment-with-locales.js . 1:​42 AM' moment.locale('de'); // Use moment now that the locale has been properly set. If you are wanting to set the offset globally, try using moment-timezone. Note that once you set an offset, it's fixed and won't change on its own (i.e there are no DST rules). If you want an actual time zone -- time in a particular location, like America/Los_Angeles, consider moment-timezone. moment().utcOffset(120);


Changing locales locally, Moment timezone. 00 use it. 00 intro · 01 node js · 02 browser · 03 require js · 04 webpack A global locale configuration can be problematic when passing around moments that moment.locale('en'); // default the locale to English var localLocale set this instance to use French localLocale.format('LLLL'); // dimanche 15  By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node.js server. To change the default time zone, use moment.tz.setDefault with a valid time zone.


Changing locale globally, By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use. To load a  Upon app startup, I set up moment's locale globally, say to German: 'moment.locale('de'). So far I had no troubles with this. Now I added some functionality where moment's formatting is used when initially rendering the app UI. The first time when this code is executed, moment uses the default English locale, regardless of the locale I set