How to Validate a Date in JavaScript

How to Validate a Date in JavaScript

There are several ways to validate a date in JavaScript. Here are four of the most common methods:

There are several ways to validate a date in JavaScript. Here are four of the most common methods:

1. Check if a Date is valid using JavaScript

The simplest way to validate a date is to use the JavaScript Date object. To do this, you can create a new Date object and then check the getTime() method. If the getTime() method returns a number, then the date is valid. Otherwise, the date is invalid.

Here is an example:

function isDateValid(date) {
  return new Date(date).getTime() !== NaN;
}

const validDate = "2023-11-09";
const invalidDate = "2023-13-32";

console.log(isDateValid(validDate)); // trueconsole.log(isDateValid(invalidDate)); // false

2. Check if a Date is valid using Object.toString()

Another way to validate a date is to use the Object.toString() method. This method returns a string that represents the type of the object. If the object is a valid Date object, then the Object.toString() method will return the string "[object Date]".

Here is an example:

function isDateValid(date) {
  return Object.toString(date) === "[object Date]";
}

const validDate = new Date();
const invalidDate = "2023-13-32";

console.log(isDateValid(validDate)); // trueconsole.log(isDateValid(invalidDate)); // false

3. Check if a Date is valid using duck-typing

Duck-typing is a programming style where the type of an object is determined by its behavior. To validate a date using duck-typing, you can check if the object has the following properties:

  • getTime(): This property returns the number of milliseconds since the Unix epoch.
  • setYear(): This property sets the year of the date.
  • setMonth(): This property sets the month of the date.
  • setDate(): This property sets the day of the date.

If the object has all of these properties, then it is likely a valid Date object.

Here is an example:

function isDateValid(date) {
  return typeof date.getTime === "function" &&
    typeof date.setYear === "function" &&
    typeof date.setMonth === "function" &&
    typeof date.setDate === "function";
}

const validDate = new Date();
const invalidDate = "2023-13-32";

console.log(isDateValid(validDate)); // trueconsole.log(isDateValid(invalidDate)); // false

4. Validate a Date formatted as YYYY-MM-DD in JavaScript

To validate a date formatted as YYYY-MM-DD, you can use the following regular expression:

/^\d{4}-\d{2}-\d{2}$/

This regular expression will match any string that contains four digits, followed by a hyphen, followed by two digits, followed by a hyphen, and then another two digits.

Here is an example:

function isDateValid(date) {
  return /^\d{4}-\d{2}-\d{2}$/.test(date);
}

const validDate = "2023-11-09";
const invalidDate = "2023-13-32";

console.log(isDateValid(validDate)); // trueconsole.log(isDateValid(invalidDate)); // false

5. Validate a Date formatted as DD/MM/YYYY in JavaScript

To validate a date formatted as DD/MM/YYYY, you can use the following regular expression:

/^\d{2}\/\d{2}\/\d{4}$/

This regular expression will match any string that contains two digits, followed by a slash, followed by two digits, followed by a slash, and then another four digits.

Here is an example:

function isDateValid(date) {
  return /^\d{2}\/\d{2}\/\d{4}$/.test(date);
}

const validDate = "09/11/2023";
const invalidDate = "32/13/2023";

console.log

# Details

Published on January 31, 2024 3 min read

Javascripts