Navigate back to the homepage

How to check if a string is a date in javascript?

Infinitbility
JavaScript
May 24th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show how you validate string is a valid date or not in javascript, here I will use the Date.parse() method to check if the string is a valid date or not.

The Date.parse() method parses a string representation of a date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

Let’s start today’s tutorial How to check if a string is a date in javascript?

Before going to code, let’s understand what we are going to do?

  1. Create two date string variables one is a valid date string and the second is a common string
  2. Use Date.parse() on both variable
  3. Console which string is a valid
1// Create two date string variable one is valid date string and second is common string
2const validDateStr = "2020-01-01";
3const inValidDateStr = "abc";
4
5// Use `Date.parse()` on both variable
6// Console which string is a valid
7if(Date.parse(validDateStr)){
8 console.log("validDateStr is a valid date");
9}
10
11if(Date.parse(inValidDateStr)){
12 console.log("inValidDateStr is a valid date");
13}

When running the above program it should console 'validDateStr is a valid date' in the log. let’s check the output.

javascript, check if a string is a date example
javascript, check if a string is a date example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is a number in javascript?

Example of check if a string is a number in javascript with step by step guide.

May 24th, 2022 · 1 min read

How to find capital letters in a string javascript?

Example of find capital letters in a string in javascript with step by step guide.

May 23rd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]