Where to use it
Moment was designed to work both in the browser and in Node.js.
All code should work in both of these environments, and all unit tests are run in both of these environments.
Currently the following browsers are used for the ci system: Chrome on Windows XP, IE 8, 9, and 10 on Windows 7,IE 11 on Windows 10, latest Firefox on Linux, and latest Safari on OSX 10.8 and 10.11.
If you want to try the sample codes below, just open your browser's console and enter them.
Node.js
npm install moment
var moment = require('moment');
moment().format();
Note: In 2.4.0, the globally exported moment object was deprecated.It will be removed in next major release.
Browser
<script src="moment.js"></script>
<script>
moment().format();
</script>
Moment.js is available on cdnjs.com and on jsDelivr.
Bower
bower install --save moment
Notable files are moment.js
, locale/*.js
and min/moment-with-locales.js
.
Require.js
We strongly recommend readingthisif you plan to use moment with Require.js. Also upgrade to 2.14.0 or abovefor best experience.
As a start, you might have aquired moment through bower or node_modules oranything else that places moment.js together with a locales directory in a basefolder. Then you should use a tool likeadapt-pkg-main, or manually —using packages config.
requirejs.config({
packages: [{
name: 'moment',
// This location is relative to baseUrl. Choose bower_components
// or node_modules, depending on how moment was installed.
location: '[bower_components|node_modules]/moment'
main: 'moment'
}]
});
With the above setup, you can require the core with moment
and de
localewith moment/locale/de
.
// only needing core
define(['moment'], function (moment) {
console.log(moment().format('LLLL')); // 'Friday, June 24, 2016 1:42 AM'
});
// core with single locale
define(['moment', 'moment/locale/de'], function (moment) {
moment.locale('de');
console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});
// core with all locales
define(['moment/min/moment-with-locales'], function (moment) {
moment.locale('de');
console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
});
// async load locale
define(['require', 'moment'], function(require, moment) {
// Inside some module after the locale is detected. This is the
// case where the locale is not known before module load time.
require(['moment/locale/de'], function(localeModule) {
// here the locale is loaded, but not yet in use
console.log(moment().format('LLLL')); // 'Friday, June 24, 2016 1:42 AM'
moment.locale('de');
// Use moment now that the locale has been properly set.
console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
})
});
For more complicated use cases please read excellent explanation by @jrburke.
Moment will still create a moment
global, which is useful to plugins and other third-party code. If you wish to squash that global, use the noGlobal
option on the module config.
require.config({
config: {
moment: {
noGlobal: true
}
}
});
If you don't specify noGlobal
then the globally exported moment will printa deprecation warning. From next major release you'll have to export ityourself if you want that behavior.
For version 2.5.x, in case you use other plugins that rely on Moment but arenot AMD-compatible you may need to add wrapShim: true
to your r.js config.
Note: To allow moment.js plugins to be loaded in requirejs environments, moment is created as a named module. Because of this, moment must be loaded exactly as as "moment"
, using paths
to determine the directory. Requiring moment with a path like "vendor\moment"
will return undefined
.
Note: From version 2.9.0 moment exports itself as an anonymous module,so if you're using only the core (no locales / plugins), then you don't needconfig if you put it on a non-standard location.
NuGet
Install-Package Moment.js
meteor
meteor / atmosphere/ momentjs:moment
meteor add momentjs:moment
Browserify
npm install moment
var moment = require('moment');
moment().format();
Note: There is a bug that prevents moment.locale
from being loaded.
var moment = require('moment');
moment.locale('cs');
console.log(moment.locale()); // en
Use the workaround below
var moment = require('moment');
require('moment/locale/cs');
console.log(moment.locale()); // cs
In order to include all the locales
var moment = require('moment');
require("moment/min/locales.min");
moment.locale('cs');
console.log(moment.locale()); // cs
Webpack
npm install moment
var moment = require('moment');
moment().format();
Note: By default, webpack bundles all Moment.js locales (in Moment.js 2.18.1, that’s 160 minified KBs). To strip unnecessary locales and bundle only the used ones, add moment-locales-webpack-plugin
:
// webpack.config.js
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
module.exports = {
plugins: [
// To strip all locales except “en”
new MomentLocalesPlugin(),
// Or: To strip all locales except “en”, “es-us” and “ru”
// (“en” is built into Moment and can’t be removed)
new MomentLocalesPlugin({
localesToKeep: ['es-us', 'ru'],
}),
],
};
There are other resources to optimize Moment.js with webpack, for example this one.
Typescript2.13.0+
As of version 2.13.0, Moment includes a typescript definition file.
Install via NPM
npm install moment
Import and use in your Typescript file
import * as moment from 'moment';
let now = moment().format('LLLL');
Note: If you have trouble importing moment
For Typescript 2.x try adding "moduleResolution": "node"
in compilerOptions
in your tsconfig.json
file and then use any of the below syntax
import * as moment from 'moment';
import moment = require('moment');
For Typescript 1.x try adding "allowSyntheticDefaultImports": true
in compilerOptions
in your tsconfig.json
file and then use the syntax
import moment from 'moment';
Locale Import
To use moment.locale
you first need to import the language you are targeting.
import * as moment from 'moment';
import 'moment/locale/pt-br';
console.log(moment.locale()); // en
moment.locale('fr');
console.log(moment.locale()); // en
moment.locale('pt-BR');
console.log(moment.locale()); // pt-BR
System.js
To load moment, place it in the path specified by your System.config in the baseURL configuration.Then import it into your page.
<script src="system.js"></script>
<script>
System.config({
baseURL: '/app'
});
System.import('moment.js');
</script>
If you need moment to be loaded as global, you can do this with the meta configuration:
System.config({
meta: {
'moment': { format: 'global' }
}
});
Alternatively, to provide Moment as a global to only a specific dependency, you can do this:
System.config({
meta: {
'path/to/global-file.js': {
globals: {
moment: 'moment'
}
}
}
});
Other
To use under Java/Rhino, see these instructions.
To use in Demandware, see these instructions.
Troubleshooting
If you are having any troubles, the first place to check is the guides.
If you don't find what you are looking for there, try asking a question on Stack Overflow with the momentjs
tag.
Note: More than half of the issues seen on Stack Overflow can be answered by this blog post.
You can also use the GitHub issue tracker to find related issues or open a new issue.
In addition, Moment has a Gitter where the internal team is frequently available.
For general troubleshooting help, Stack Overflow is the preferred forum.Moment's maintainers are very active on Stack Overflow, as are several other knowledgeable users. The fastest response will be there.