Node.js Date.compile() API
Last Updated :
27 Jan, 2023
The date-and-time.Date.compile() is a minimalist collection of functions for manipulating JS date and time module which is used to compile a format string for the parser.
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:
compile(formatString)
Parameters: This method takes the format string as the parameter.
Return Value: This method returns the formatted string date.
Example 1:
index.js
const date = require( 'date-and-time' )
const now = new Date( '07-03-2021' );
const pattern = date.compile( 'D/M/YYYY' );
const value = date.format(now,pattern)
console.log( "Formatted date and time : " + value)
|
Run the index.js file using the following command:
node index.js
Output:
Formatted date and time : 1/1/1970
Example 2:
index.js
const date = require( 'date-and-time' )
const now = new Date( '07-03-2021' );
const pattern = date.compile( 'M/D/YYYY' );
const value = date.parse(now.toLocaleDateString(), pattern);
console.log( "Formatted date and time: " + value)
|
Run the index.js file using the following command:
node index.js
Output:
Formatted date and time:
Sat Jul 03 2021 00:00:00 GMT+0530 (India Standard Time)
Reference: https://github.com/knowledgecode/date-and-time
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...