VOOZH about

URL: https://www.geeksforgeeks.org/node-js/locale-detection-with-momentjs/

⇱ Locale detection with Moment.js - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Locale detection with Moment.js

Last Updated : 23 Jul, 2025

Moment.js is a popular JavaScript library for parsing, validating, manipulating, and displaying dates and times. One powerful feature of Moment.js is its ability to handle different locales, making it easier to format dates and times according to specific regions and languages.

In this article, we will explore different approaches to detect and apply locales in Moment.js.

Prerequisites

Install Lodash:

Open your terminal and navigate to your project directory. Run the following command to install Lodash:

npm install lodash

Below are the approaches for locale detection with Moment.js:

1. Using the Default Locale

By default, Moment.js uses the locale setting of the environment in which it is running. If no locale is set explicitly, it defaults to English (en).

Example: In below example we are displaying the default locale using moment.locale() method.


Output:

Current Locale: en
Friday, September 6, 2024 9:25 AM

2. Setting Locale Manually

Moment.js allows us to manually set a locale for your application. This can be done using the moment.locale() method. Moment.js supports various locales like en, fr, de, and more.

Example: In this example we will set the locale manually using the moment.locale() method.

Output:

Current Locale: fr
vendredi 6 septembre 2024 09:30
Comment
Article Tags:

Explore