📌  相关文章
📜  Node.js 日期和时间 Date.locale() 方法

📅  最后修改于: 2022-05-13 01:56:34.056000             🧑  作者: Mango

Node.js 日期和时间 Date.locale() 方法

date-and-time.Date.locale()方法用于获取或将语言环境从一种语言切换到另一种语言。

所需模块:通过 npm 安装模块或在本地使用它。

通过使用 npm:

npm install date-and-time --save

通过使用 CDN 链接:

句法:

locale(])

参数:此方法将语言环境和代码作为参数。

返回值:此方法返回日期和时间对象的当前语言环境。

示例 1:

index.js
// 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)


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'))


使用以下命令运行index.js文件:

node index.js

输出:

current locale :- en

示例 2:更改语言环境

文件名:index.js

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'))

使用以下命令运行index.js文件:

node index.js

输出:

Change local es

参考:https://github.com/knowledgecode/date-and-time#localecode-locale