Node.js Date.format() API
Last Updated :
21 Jan, 2022
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.
npm install date-and-time --save
<script src="/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:
index.js
const date = require( 'date-and-time' )
const now = new Date();
const value = date.format(now, 'YYYY/MM/DD HH:mm:ss' );
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:
index.js
const date = require( 'date-and-time' )
const value = date.format(( new Date( 'December 17, 1995 03:24:00' )),
'YYYY/MM/DD HH:mm:ss' );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...