Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptLodash _.isValidDate() Method

Lodash _.isValidDate() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.isValidDate() method is used to check whether the given value is a valid date. The value is checked if it is both an instance of the Date object and this Date object represents a valid date.

Note: This method does not verify whether the original input to Date is a real date. For instance, the date string “02/30/2014” is considered a valid date because the Date object interprets it as the date representation “03/02/2014”, which is correct. A library like Moment.js can be used to validate the strings representing a date.

Syntax:

_.isValidDate( value )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • value: This parameter holds the value that needs to be checked for a valid date.

Return Value: This method returns a Boolean value. It returns true if the given value is a valid date, otherwise, it returns false.

Note: This will not work in normal JavaScript because it requires the lodash contrib library to be installed. The Lodash contrib library can be installed using npm install lodash-contrib –save.

Example 1: 

Javascript




// Defining Lodash variable 
const _ = require('lodash-contrib'); 
  
var validDate = new Date("10/02/2014");
var invalidDate = new Date("10/32/2014");
  
// Checking for Valid Date Object 
console.log("The Value of Valid Date : " +
  _.isValidDate(validDate));
console.log("The Value of Invalid Date : " +
  _.isValidDate(invalidDate));


Output:

The Value of Valid Date : true
The Value of Invalid Date : false

Example 2:

Javascript




// Defining Lodash-contrib variable 
const _ = require('lodash-contrib'); 
      
var val = "World War 2"
  
// Checking for Valid Date Object 
console.log("The Value of Date : " +
  _.isValidDate(val));


Output:

The Value of Date : false

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments