Node.js Date.isValid() API
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.
- By using npm.
npm install date-and-time --save
- By using CDN link.
<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
// Node.js program to demonstrate the // Date.isValid() method // Importing date-and-time module const date = require( 'date-and-time' ) // Validating the terms // by using date.isValid() method const status = date.isValid( '29-02-2015' , 'DD-MM-YYYY' ); // Display the 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 not invalid
Example 2:
index.js
// Node.js program to demonstrate the // Date.isValid() method // Importing date-and-time module const date = require( 'date-and-time' ) // Pre parsing the date and time // by using preparse() method const result = date.preparse( '2015/01/02 23:14:05' , 'YYYY/MM/DD HH:mm:ss' ); // Validating the terms // by using date.isValid() method const status = date.isValid(result); // Display the 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