Node.js Date.isValid() API
Last Updated :
27 Jan, 2023
The date-and-time.Date.isValid() is a minimalist collection of functions for manipulating JS date and time module which is used to validate the particular date and time with its string format.
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:
isValid(arg1[, arg2])
Parameters: This method takes the following arguments as parameters:
- arg1: It is the date and time object.
- arg2: It is the string format of the given date.
Return Value: This method returns true if and only if the terms are validated.
Example 1:
index.js
const date = require( 'date-and-time' )
const status = date.isValid( '29-02-2015' , 'DD-MM-YYYY' );
if (status)
console.log( "Date is valid" )
else
console.log( "Date is not invalid" )
|
Run the index.js file using the following command:
node index.js
Output:
Date is not invalid
Example 2:
index.js
const date = require( 'date-and-time' )
const result = date.preparse( '2015/01/02 23:14:05' , 'YYYY/MM/DD HH:mm:ss' );
const status = date.isValid(result);
if (status)
console.log( "Date is valid" )
else
console.log( "Date is not invalid" )
|
Run the index.js file using the following command:
node index.js
Output:
Date is valid
Reference: https://github.com/knowledgecode/date-and-time
Share your thoughts in the comments
Please Login to comment...