Node.js Date.format() API
Last Updated :
08 Jan, 2025
Improve
The date-and-time.Date.format() is a minimalist collection of functions for manipulating JS date and time module which is used to format the date according to a certain pattern.
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="/https/www.geeksforgeeks.org/path/to/date-and-time.min.js"></script>
Syntax:
format(dateObj, formatString[, utc])
Parameters: This method takes the following arguments as parameters:
- dateObj: It is the object of the date.
- formatString: It is the new string format in which date will be shown.
Return Value: This method returns formatted date and time.
Example 1:
// 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)
Run the index.js file using the following command:
node index.js
Output:
current date and time : 2021/03/07 12:13:46
Example 2:
// 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)
Run the index.js file using the following command:
node index.js
Output:
date and time : 1995/12/17 03:24:00
Reference: https://github.com/knowledgecode/date-and-time