The date-and-time.Date.locale() method is used to get or switch the locale from one into another language.
Required Module: Install the module by npm or used it locally.
By using npm:
npm install date-and-time --save
By using CDN link:
<script src="/path/to/date-and-time.min.js"></script>
Syntax:
locale([code 1="locale" language="[,"][/code]])
Parameters: This method takes the locale and code as a parameter.
Return Value: This method returns the current locale of the date and time object.
Example 1:
// Node.js program to demonstrate the
// Date.locale() APi
// Importing date-and-time module
const date = require('date-and-time')
// Getting the locale value
const value = date.locale();
// display the result
console.log("current locale :- " + value)
Run index.js file using below command:
node index.js
Output:
current locale :- en
Example 2: Changing localeÂ
Filename: index.jsÂ
// Node.js program to demonstrate the
// Date.locale() APi
// Importing date-and-time module
const date = require('date-and-time')
// Changing the local to es spanish
console.log("Change local "+date.locale('es'))
Run index.js file using below command:
node index.js
Output:
Change local es
Reference: https://github.com/knowledgecode/date-and-time#localecode-locale