How to check if a string is a valid date using regular expression or regex in JavaScript?
Tags: javascript
Published: 2021-06-05 00:00:00

---
  title: How to check if a string is a valid date using regular expression or regex in JavaScript?
  published: true
  tags: JavaScript
  date: Sat Jun 05 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/check-string-is-valid-date-regex-javascript
  cover_image: https://melvingeorge.me/_next/static/images/main-bdb4f8874012e3d7951cc244cd92666d.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/check-string-is-valid-date-regex-javascript)
  
To check if a string is a valid date using regex in JavaScript, we can use a regex expression to match the symbols like `/`, `.`, `-` and the numbers in between them in the format of `dd/mm/yyyy` , `dd-mm-yyyy` or `dd.mm.yyyy`.

### TL;DR

```js
// Regular expression to check if string is valid date
const regexExp = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/gi;

// String with valid date separated by dash
const str = "01-01-2021";

regexExp.test(str); // true
```

First let's see the regular expression which we will use to match the strings. It can be done like this,

```js
// Regular expression to check if string is valid date
const regexExp = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/gi;
```

This would match for date strings in the format of `dd/mm/yyyy` , `dd-mm-yyyy` or `dd.mm.yyyy`.

Now let's write a string with valid date like this,

```js
// Regular expression to check if string is valid date
const regexExp = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/gi;

// String with valid date separated by dash
const str = "01-01-2021";
```

Now to test the string, we can use the `test()` method available in the regular expression we defined. It can be done like this,

```js
// Regular expression to check if string is valid date
const regexExp = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/gi;

// String with valid date separated by dash
const str = "01-01-2021";

regexExp.test(str); // true
```

- The `test()` method will accept a `string` type as an argument to test for a match.
- The method will return boolean `true` if there is a match using the regular expression and `false` if not.

See the above example live in [JSBin](https://jsbin.com/yuvozococi/2/edit?js,console).

If you want this as a utility function which you can reuse, here it is,

```js
/* Check if string is valid date */
function checkIfValidDate(str) {
  // Regular expression to check if string is valid date
  const regexExp = /(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})/gi;

  return regexExp.test(str);
}

// Use the function
checkIfValidDate("01-01-2021"); // true
checkIfValidDate("32-01-2021"); // false
```

That's all! 😃

### Feel free to share if you found this useful 😃.

---