📜  Node.js Date.format() API

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

Node.js Date.format() API

date-and-time.Date.format()是用于操作 JS 日期和时间模块的函数的极简集合,用于根据特定模式格式化日期。

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

  • 通过使用 npm。
npm install date-and-time --save
  • 通过使用 CDN 链接。

句法:

format(dateObj, formatString[, utc])

参数:此方法采用以下参数作为参数:

  • dateObj:是日期的对象。
  • formatString:这是显示日期的新字符串格式。

返回值:此方法返回格式化的日期和时间。

示例 1:

index.js
// Node.js program to demonstrate the  
// Date.format() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now  =  new Date();
  
// Formatting the date and time
// by using date.format() method
const value = date.format(now,'YYYY/MM/DD HH:mm:ss');
  
// Display the result
console.log("current date and time : " + value)


index.js
// Node.js program to demonstrate the  
// Date.format() method
  
// Importing module
const date = require('date-and-time')
  
// Formatting the date and time
// by using date.format() method
const value = date.format((new Date('December 17, 1995 03:24:00')),
  'YYYY/MM/DD HH:mm:ss');
  
// Display the result
console.log("date and time : " + value)


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

node index.js

输出:

current date and time : 2021/03/07 12:13:46

示例 2:

index.js

// Node.js program to demonstrate the  
// Date.format() method
  
// Importing module
const date = require('date-and-time')
  
// Formatting the date and time
// by using date.format() method
const value = date.format((new Date('December 17, 1995 03:24:00')),
  'YYYY/MM/DD HH:mm:ss');
  
// Display the result
console.log("date and time : " + value)

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

node index.js

输出:

date and time : 1995/12/17 03:24:00

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