You can load multiple locales and easily switch between them.
In addition to assigning a global locale, you can assign a locale to a specificmoment.
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 .
More details on each of the parts of the locale bundle can be found in the customization section.
moment.locale('fr', {
months : 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_'),
monthsShort : 'janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.'.split('_'),
monthsParseExact : true,
weekdays : 'dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi'.split('_'),
weekdaysShort : 'dim._lun._mar._mer._jeu._ven._sam.'.split('_'),
weekdaysMin : 'Di_Lu_Ma_Me_Je_Ve_Sa'.split('_'),
weekdaysParseExact : true,
longDateFormat : {
LT : 'HH:mm',
LTS : 'HH:mm:ss',
L : 'DD/MM/YYYY',
LL : 'D MMMM YYYY',
LLL : 'D MMMM YYYY HH:mm',
LLLL : 'dddd D MMMM YYYY HH:mm'
},
calendar : {
sameDay : '[Aujourd’hui à] LT',
nextDay : '[Demain à] LT',
nextWeek : 'dddd [à] LT',
lastDay : '[Hier à] LT',
lastWeek : 'dddd [dernier à] LT',
sameElse : 'L'
},
relativeTime : {
future : 'dans %s',
past : 'il y a %s',
s : 'quelques secondes',
m : 'une minute',
mm : '%d minutes',
h : 'une heure',
hh : '%d heures',
d : 'un jour',
dd : '%d jours',
M : 'un mois',
MM : '%d mois',
y : 'un an',
yy : '%d ans'
},
dayOfMonthOrdinalParse : /\d{1,2}(er|e)/,
ordinal : function (number) {
return number + (number === 1 ? 'er' : 'e');
},
meridiemParse : /PD|MD/,
isPM : function (input) {
return input.charAt(0) === 'M';
},
// In case the meridiem units are not separated around 12, then implement
// this function (look at locale/id.js for an example).
// meridiemHour : function (hour, meridiem) {
// return /* 0-23 hour, given meridiem token and hour 1-12 */ ;
// },
meridiem : function (hours, minutes, isLower) {
return hours < 12 ? 'PD' : 'MD';
},
week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // Used to determine first week of the year.
}
});
Details about week.dow
and week.doy
can be found in the 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.
moment.locale('fr');
moment.locale('en');
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.
moment.locale('fr');
var m = moment(1316116057189);
m.fromNow(); // il y a une heure
moment.locale('en');
m.fromNow(); // il y a une heure
moment.locale
returns the locale used. This is useful because Moment won't change locales if it doesn't know the one you specify.
moment.locale('fr'); // 'fr'
moment.locale('tq'); // 'fr'
You may also specify a list of locales, and Moment will use the first one it has localizations for.
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
.
moment.locale('en-NZ'); // 'en'
Finally, Moment will search intelligently through an array of locales and their substrings.
moment.locale(['en-NZ', 'en-AU']); // 'en-au', not 'en'
Changing locales locally1.7.0+
// From version 2.8.1 onward
moment().locale(String|Boolean);
// Deprecated version 2.8.1
moment().lang(String|Boolean);
A global locale configuration can be problematic when passing around moments that may need to be formatted into different locale.
moment.locale('en'); // default the locale to English
var localLocale = moment();
localLocale.locale('fr'); // set this instance to use French
localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01
moment().format('LLLL'); // Sunday, July 15 2012 11:01 AM
moment.locale('es'); // change the global locale to Spanish
localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01
moment().format('LLLL'); // Domingo 15 Julio 2012 11:01
localLocale.locale(false); // reset the instance locale
localLocale.format('LLLL'); // Domingo 15 Julio 2012 11:01
moment().format('LLLL'); // Domingo 15 Julio 2012 11:01
var fr = moment().locale('fr');
fr.localeData().months(moment([2012, 0])) // "janvier"
fr.locale('en');
fr.localeData().months(moment([2012, 0])) // "January"
If you need to access the locale data for a moment, this is the preferred way to do so.
As of 2.3.0, you can also specify an array of locale identifiers. It works the same way it does in the global locale configuration.
Loading locales in NodeJS1.0.0+
Loading locales in NodeJS is super easy. If there is a locale file in moment-root/locale/
named after that key, the first call to moment.locale
will load it.
var moment = require('moment');
moment.locale('fr');
moment(1316116057189).fromNow(); // il y a une heure
If you want your locale supported, create a pull request to the develop
branch with the .
// From 2.8.1 onward
moment.locale(String, Object);
// Deprecated in 2.8.1
moment.lang(String, Object);
Loading locales in the browser just requires you to include the locale files. Be sure to specify the charset to prevent encoding issues.
<script src="moment.js"></script>
<script src="locale/fr.js" charset="UTF-8"></script>
<script src="locale/pt.js" charset="UTF-8"></script>
<script>
moment.locale('fr'); // Set the default/global locale
// ...
</script>
There are minified versions of all locales together:
<script src="moment.js"></script>
<script src="min/locales.js" charset="UTF-8"></script>
To minimize HTTP requests, use our Grunt task to compile with a custom list of locales:
grunt transpile:fr,it
<script src="min/moment-with-locales.custom.js" charset="UTF-8"></script>
If you are using JSPM as plugin manager, you should add the locale in your lib.
import * as moment from 'moment';
import 'moment/locale/fr';
Note: Locale files are defined in UMD style, so they should work seamlessly in all environments.
Adding your locale to Moment.js
To add your locale to Moment.js, submit a pull request with both a locale file and a test file. You can find examples in moment/src/locale/fr.js
and moment/src/test/locale/fr.js
.
To run the tests in Node.js, do npm install
, then grunt
.
If all the tests pass, submit a pull request, and thank you for contributing!
Checking the current Moment.js locale1.6.0+
// From version 2.8.1 onward
moment.locale();
// Deprecated in version 2.8.1
moment.lang();
moment.locale('en'); // set to english
moment.locale(); // returns 'en'
moment.locale('fr'); // set to french
moment.locale(); // returns 'fr'
As of version 2.12.0 it is possible to list all locales that have been loaded and are available to use:
moment.locales()
It is sometimes useful to get the list of months or weekdays in a locale, for example when populating a dropdown menu.
moment.months();
Returns the list of months in the current locale.
[ 'January',
'March',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December' ]
Similarly, moment.monthsShort
returns abbreviated month names, and moment.weekdays
, moment.weekdaysShort
, moment.weekdaysMin
return lists of weekdays.
You can pass an integer into each of those functions to get a specific month or weekday.
moment.weekdays(3); // 'Wednesday'
As of 2.13.0 you can pass a bool as the first parameter of the weekday functions. If true, the weekdays will be returned in locale specific order.For instance, in the Arabic locale, Saturday is the first day of the week, thus:
moment.locale('ar');
moment.weekdays(true); // lists weekdays Saturday-Friday in Arabic
moment.weekdays(true, 2); //will result in Monday in Arabic
Note: Absent the locale specific parameter, weekdays always have Sunday as index 0, regardless of the local first day of the week.
Some locales make special considerations into account when formatting month names. For example, Dutch formats month abbreviations without a trailing period, but only if it's formatting the month between dashes. The months
method supports passing a format in so that the months will be listed in the proper context.
moment.locale('nl');
moment.monthsShort(); // ['jan.', 'feb.', 'mrt.', ...]
moment.monthsShort('-MMM-'); // [ 'jan', 'feb', 'mrt', ...]
And finally, you can combine both the format option and the integer option.
moment.monthsShort('-MMM-', 3); // 'apr'
Accessing locale specific functionality2.8.0+
localeData = moment.localeData()
localeData.months(Moment)
localeData.months()
localeData.monthsShort(Moment)
localeData.monthsShort()
localeData.monthsParse(String)
localeData.weekdays(Moment)
localeData.weekdays()
localeData.weekdays(Boolean) ## Added 2.24.0, sorts weekdays by locale
localeData.weekdaysShort(Moment)
localeData.weekdaysShort()
localeData.weekdaysShort(Boolean) ## Added 2.24.0, sorts weekdays by locale
localeData.weekdaysMin(Moment)
localeData.weekdaysMin()
localeData.weekdaysMin(Boolean) ## Added 2.24.0, sorts weekdays by locale
localeData.weekdaysParse(String)
localeData.longDateFormat(String)
localeData.isPM(String)
localeData.meridiem(Number, Number, Boolean)
localeData.calendar(String, Moment)
localeData.relativeTime(Number, Boolean, String, Boolean)
localeData.pastFuture(Number, String)
localeData.ordinal(Number)
localeData.preparse(String)
localeData.postformat(String)
localeData.week(Moment)
localeData.invalidDate()
localeData.firstDayOfWeek()
localeData.firstDayOfYear()
You can access the properties of the currently loaded locale through themoment.localeData(key)
function. It returns the current locale or a localewith the given key:
// get current locale
var currentLocaleData = moment.localeData();
var frLocaleData = moment.localeData('fr');
The returned object has the following methods:
localeData.months(aMoment); // full month name of aMoment
localeData.monthsShort(aMoment); // short month name of aMoment
localeData.monthsParse(longOrShortMonthString); // returns month id (0 to 11) of input
localeData.weekdays(aMoment); // full weekday name of aMoment
localeData.weekdaysShort(aMoment); // short weekday name of aMoment
localeData.weekdaysMin(aMoment); // min weekday name of aMoment
localeData.weekdaysParse(minShortOrLongWeekdayString); // returns weekday id (0 to 6) of input
localeData.longDateFormat(dateFormat); // returns the full format of abbreviated date-time formats LT, L, LL and so on
localeData.isPM(amPmString); // returns true iff amPmString represents PM
localeData.meridiem(hours, minutes, isLower); // returns am/pm string for particular time-of-day in upper/lower case
localeData.calendar(key, aMoment); // returns a format that would be used for calendar representation. Key is one of 'sameDay', 'nextDay', 'lastDay', 'nextWeek', 'prevWeek', 'sameElse'
localeData.relativeTime(number, withoutSuffix, key, isFuture); // returns relative time string, key is on of 's', 'm', 'mm', 'h', 'hh', 'd', 'dd', 'M', 'MM', 'y', 'yy'. Single letter when number is 1.
localeData.pastFuture(diff, relTime); // convert relTime string to past or future string depending on diff
localeData.ordinal(number); // convert number to ordinal string 1 -> 1st
localeData.preparse(str); // called before parsing on every input string
localeData.postformat(str); // called after formatting on every string
localeData.week(aMoment); // returns week-of-year of aMoment
localeData.invalidDate(); // returns a translation of 'Invalid date'
localeData.firstDayOfWeek(); // 0-6 (Sunday to Saturday)
localeData.firstDayOfYear(); // 0-15 Used to determine first week of the year.
Details about can be found in the customization section.
Pseudo Locale2.13.0+
As of version 2.13.0 moment optionally includes a pseudo locale. This locale will populate the dates with very obviously changed data.Pseudo locales can be useful when testing, as they make obvious what data has and has not been localized. Just include the pseudo-locale, and set moment's locale to x-pseudo.Text from Moment will be very easy to spot.