Node.js Date.preparse() API
Last Updated :
12 Mar, 2021
The date-and-time.Date.preparse() is a minimalist collection of functions for manipulating JS date and time module which is used to pre-parse 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="/https/www.geeksforgeeks.org/path/to/date-and-time.min.js"></script>
Syntax:
preparse(dateString, arg)
Parameters: This method takes the following arguments as parameters:
- dateString: It is the string object of the date.
- arg: It is the required date format.
Return Value: This method returns parsed date and time.
Example 1:
index.js
// Node.js program to demonstrate the
// Date.preparse() method
// Importing module
const date = require('date-and-time')
// Creating object of current date and time
// by using Date()
const now = new Date('07-03-2021');
// Pre parsing the date and time
// by using date.preparse() method
const value = date.preparse(now.toLocaleDateString(),'D/M/YYYY');
// Display the result
console.log(value)
Run the index.js file using the following command:
node index.js
Output:
{
Y: 2021,
M: 3,
D: 7,
H: 0,
A: 0,
h: 0,
m: 0,
s: 0,
S: 0,
Z: 0,
_index: 8,
_length: 8,
_match: 3
}
Example 2:
index.js
// Node.js program to demonstrate the
// Date.preparse() method
// Importing module
const date = require('date-and-time')
// Pre parsing the date and time
// by using date.preparse() method
const value = date.preparse('23:14:05 GMT+0900','HH:mm:ss [GMT]Z');
// Display the result
console.log("pre parsed date and time : " + value._length)
Run the index.js file using the following command:
node index.js
Output:
pre parsed date and time : 17
Reference: https://github.com/knowledgecode/date-and-time