JavaScript

How to check if a date is valid or not using JavaScript

Whenever data is entered into a variable in JavaScript, it gets assigned its corresponding data type. For instance, if you enter 5, the data type would be int. But if you enter “5”, it will be stored as a string. This sort of check on the type of data exists only for the common data types. But what about some uncommon ones such as dates? The system does not automatically detect whether an entered date is valid or invalid.

In this article, let us look at how a date variable can be verified for whether it is valid.

How can it be verified if a date is valid or not?

In JavaScript, the Date() function is used to create and assign a date variable. A few different functions exist which may be utilized for checking purposes. There are two meaningful assessments that should be made. If both of those two prove to be true, the date is valid. Some light is shed on what these two checks are down below.

The “instanceof” keyword

This keyword is used to verify whether a certain variable is created from a predefined constructor or not. The keyword returns a boolean value of true if the specified variable is indeed constructed from the specified constructor.

Let’s take an example down below:

In this scenario, since x is an object created using the Date() constructor, the instanceof keyword returns true.

The “isNaN” keyword

This keyword stands for “is not a number”. It is used to verify whether a variable in use is a number. In the case that the variable turns out not to be a number(isNaN returns true) then it converts it into a number. If a date is converted to a number, it converts to milliseconds. So if isNaN turns out to be false, the variable is a date.

Check out the example below for clarification:

Since the variable being converted is a Date(), it returns the milliseconds. This indicates that the isNaN will return false.

Now let’s combine our two concepts into a function together to prove that a number is a valid date. The code is given down below:

function validDate() {

var x = new Date();

return x instanceof Date && !isNaN(x);

}

console.log(validDate());

In this function a variable is declared. In the next step, the condition is checking if the instanceof is true and isNaN is false. If these two situations occur, the function returns true showing that the variable is a valid date.

Check out the examples down below:

This example shows a valid date variable being verified.

In this example, an invalid date exists, and hence the false shows that it is not a valid date.

Conclusion

The article elaborates on how a function uses the instanceof and the isNaN function to check the validity of a Date. If instanceof is true and isNaN is false then the date is valid, otherwise, it is invalid. The article explains both these keywords in great detail as well as how they are combined in the main function.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.