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

  1. npm install moment
  1. var moment = require('moment');
  2. moment().format();

Note: In 2.4.0, the globally exported moment object was deprecated.It will be removed in next major release.

Browser

  1. <script src="moment.js"></script>
  2. <script>
  3. moment().format();
  4. </script>

Moment.js is available on cdnjs.com and on jsDelivr.

Bower

bower

  1. 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.

  1. requirejs.config({
  2. packages: [{
  3. name: 'moment',
  4. // This location is relative to baseUrl. Choose bower_components
  5. // or node_modules, depending on how moment was installed.
  6. location: '[bower_components|node_modules]/moment'
  7. main: 'moment'
  8. }]
  9. });

With the above setup, you can require the core with moment and de localewith moment/locale/de.

  1. // only needing core
  2. define(['moment'], function (moment) {
  3. console.log(moment().format('LLLL')); // 'Friday, June 24, 2016 1:42 AM'
  4. });
  5. // core with single locale
  6. define(['moment', 'moment/locale/de'], function (moment) {
  7. moment.locale('de');
  8. console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  9. });
  10. // core with all locales
  11. define(['moment/min/moment-with-locales'], function (moment) {
  12. moment.locale('de');
  13. console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  14. });
  15. // async load locale
  16. define(['require', 'moment'], function(require, moment) {
  17. // Inside some module after the locale is detected. This is the
  18. // case where the locale is not known before module load time.
  19. require(['moment/locale/de'], function(localeModule) {
  20. // here the locale is loaded, but not yet in use
  21. console.log(moment().format('LLLL')); // 'Friday, June 24, 2016 1:42 AM'
  22. moment.locale('de');
  23. // Use moment now that the locale has been properly set.
  24. console.log(moment().format('LLLL')); // 'Freitag, 24. Juni 2016 01:42'
  25. })
  26. });

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.

  1. require.config({
  2. config: {
  3. moment: {
  4. noGlobal: true
  5. }
  6. }
  7. });

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: trueto 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

NuGet / Moment.js

  1. Install-Package Moment.js

meteor

meteor / atmosphere/ momentjs:moment

  1. meteor add momentjs:moment

Browserify

  1. npm install moment
  1. var moment = require('moment');
  2. moment().format();

Note: There is a bug that prevents moment.locale from being loaded.

  1. var moment = require('moment');
  2. moment.locale('cs');
  3. console.log(moment.locale()); // en

Use the workaround below

  1. var moment = require('moment');
  2. require('moment/locale/cs');
  3. console.log(moment.locale()); // cs

In order to include all the locales

  1. var moment = require('moment');
  2. require("moment/min/locales.min");
  3. moment.locale('cs');
  4. console.log(moment.locale()); // cs

Webpack

  1. npm install moment
  1. var moment = require('moment');
  2. 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:

  1. // webpack.config.js
  2. const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
  3. module.exports = {
  4. plugins: [
  5. // To strip all locales except “en”
  6. new MomentLocalesPlugin(),
  7. // Or: To strip all locales except “en”, “es-us” and “ru”
  8. // (“en” is built into Moment and can’t be removed)
  9. new MomentLocalesPlugin({
  10. localesToKeep: ['es-us', 'ru'],
  11. }),
  12. ],
  13. };

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

  1. npm install moment

Import and use in your Typescript file

  1. import * as moment from 'moment';
  2. 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

  1. import * as moment from 'moment';
  2. import moment = require('moment');

For Typescript 1.x try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax

  1. import moment from 'moment';

Locale Import

To use moment.locale you first need to import the language you are targeting.

  1. import * as moment from 'moment';
  2. import 'moment/locale/pt-br';
  3. console.log(moment.locale()); // en
  4. moment.locale('fr');
  5. console.log(moment.locale()); // en
  6. moment.locale('pt-BR');
  7. 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.

  1. <script src="system.js"></script>
  2. <script>
  3. System.config({
  4. baseURL: '/app'
  5. });
  6. System.import('moment.js');
  7. </script>

If you need moment to be loaded as global, you can do this with the meta configuration:

  1. System.config({
  2. meta: {
  3. 'moment': { format: 'global' }
  4. }
  5. });

Alternatively, to provide Moment as a global to only a specific dependency, you can do this:

  1. System.config({
  2. meta: {
  3. 'path/to/global-file.js': {
  4. globals: {
  5. moment: 'moment'
  6. }
  7. }
  8. }
  9. });

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.