Open In App

Moment.js using with Browserify

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. In this article, we will learn how to use Moment.js with Browserify.

To run the Moment.js program in Browserify, it needs to be installed first using the following command:

npm install moment

Then we need to acquire the plugin in the program so that it is ready to use.

Syntax:

let moment = require('moment');

The below examples will show how to use Moment.js with Browserify along with its configurations.

Example 1: In this example, we include all the locales in Browserify.

Javascript




// Acquiring the plugin
const moment = require("moment");
require("moment/min/locales.min");
 
// Set the locale to 'es'
moment.locale("es");
console.log(moment.locale());


Output:

es

Example 2: Due to a bug that prevents the loading of the moment.locale, we will use the workaround as shown in this example.

Javascript




// Acquiring the plugin
const moment = require("moment");
 
// Set the locale to 'cs'
moment.locale("cs");
console.log(moment.locale());


Output:

cs

References: https://momentjs.com/docs/#/use-it/browserify/


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads