Changing locale globally1.0.0+

  1. // From 2.8.1 onward
  2. moment.locale(String);
  3. moment.locale(String[]);
  4. moment.locale(String, Object);
  5. // Deprecated in 2.8.1
  6. moment.lang(String);
  7. moment.lang(String[]);
  8. moment.lang(String, Object);

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.

More details on each of the parts of the locale bundle can be found in the customization section.

  1. moment.locale('fr', {
  2. months : 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_'),
  3. monthsShort : 'janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.'.split('_'),
  4. monthsParseExact : true,
  5. weekdays : 'dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi'.split('_'),
  6. weekdaysShort : 'dim._lun._mar._mer._jeu._ven._sam.'.split('_'),
  7. weekdaysMin : 'Di_Lu_Ma_Me_Je_Ve_Sa'.split('_'),
  8. weekdaysParseExact : true,
  9. longDateFormat : {
  10. LT : 'HH:mm',
  11. LTS : 'HH:mm:ss',
  12. L : 'DD/MM/YYYY',
  13. LL : 'D MMMM YYYY',
  14. LLL : 'D MMMM YYYY HH:mm',
  15. LLLL : 'dddd D MMMM YYYY HH:mm'
  16. },
  17. calendar : {
  18. sameDay : '[Aujourd’hui à] LT',
  19. nextDay : '[Demain à] LT',
  20. nextWeek : 'dddd [à] LT',
  21. lastDay : '[Hier à] LT',
  22. lastWeek : 'dddd [dernier à] LT',
  23. sameElse : 'L'
  24. },
  25. relativeTime : {
  26. future : 'dans %s',
  27. past : 'il y a %s',
  28. s : 'quelques secondes',
  29. m : 'une minute',
  30. mm : '%d minutes',
  31. h : 'une heure',
  32. hh : '%d heures',
  33. d : 'un jour',
  34. dd : '%d jours',
  35. M : 'un mois',
  36. MM : '%d mois',
  37. y : 'un an',
  38. yy : '%d ans'
  39. },
  40. dayOfMonthOrdinalParse : /\d{1,2}(er|e)/,
  41. ordinal : function (number) {
  42. return number + (number === 1 ? 'er' : 'e');
  43. },
  44. meridiemParse : /PD|MD/,
  45. isPM : function (input) {
  46. return input.charAt(0) === 'M';
  47. },
  48. // In case the meridiem units are not separated around 12, then implement
  49. // this function (look at locale/id.js for an example).
  50. // meridiemHour : function (hour, meridiem) {
  51. // return /* 0-23 hour, given meridiem token and hour 1-12 */ ;
  52. // },
  53. meridiem : function (hours, minutes, isLower) {
  54. return hours < 12 ? 'PD' : 'MD';
  55. },
  56. week : {
  57. dow : 1, // Monday is the first day of the week.
  58. doy : 4 // Used to determine first week of the year.
  59. }
  60. });

Details about week.dow and week.doy can be found in the customization section.

Once you load a locale, it becomes the active locale. To change active locales, simply call moment.locale with the key of a loaded locale.

  1. moment.locale('fr');
  2. moment(1316116057189).fromNow(); // il y a une heure
  3. moment.locale('en');
  4. moment(1316116057189).fromNow(); // an hour ago

As of 2.21.0, Moment will console.warn if the locale is unavailable.

As of 2.8.0, changing the global locale doesn't affect existing instances.

  1. moment.locale('fr');
  2. var m = moment(1316116057189);
  3. m.fromNow(); // il y a une heure
  4. moment.locale('en');
  5. m.fromNow(); // il y a une heure
  6. moment(1316116057189).fromNow(); // an hour ago

moment.locale returns the locale used. This is useful because Moment won't change locales if it doesn't know the one you specify.

  1. moment.locale('fr'); // 'fr'
  2. moment.locale('tq'); // 'fr'

You may also specify a list of locales, and Moment will use the first one it has localizations for.

  1. moment.locale(['tq', 'fr']); // 'fr'

Moment will also try locale specifier substrings from most-specific to least-specific until it finds a locale it knows. This is useful when supplying Moment with a locale string pulled from the user's environment, such as window.navigator.language.

  1. moment.locale('en-NZ'); // 'en'

Finally, Moment will search intelligently through an array of locales and their substrings.

  1. moment.locale(['en-NZ', 'en-AU']); // 'en-au', not 'en'