Python Check if String is Datetime

Python Check if String is Datetime

To check if a string is DateTime in Python, we can use:

  • dateutil.parse() method

  • datetime() method

Check if String is Datetime using parse()

parse() is a method of the dateutil module used to parse most formats to represent a date or time. Therefore we'll use parse() to check if the string is a DateTime

In the following example, we'll check if my_str is DateTime.

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "1995-12-12" # 👉️ String

# 👇 Check if my_str is DateTime
try: 
    parse(my_str, fuzzy=False) # 👉️ (fuzzy=False) String is DateTime
    print(True)

except ValueError:
    print(False)

Output:

True

Let me explain what we did:

  1. We've imported the parse() module
  2. We've  defined my_str to check it
  3. We've used try and except to handle the parse() errors
  4. We've checked my_str using parse() with Set fuzzy=False

If you set fuzzy=False, parse() will check whether the string is a date, time, or both. Otherwise, If you set fuzzy=True will check if the string contains a date or time or both.

Finally, if the string is the Datetime, the program returns True. If not, it returns False.

Let's see another example of a Date:

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "1995/12/12" # 👉️ String

# 👇 Check if my_str is DateTime
try: 
    parse(my_str, fuzzy=False)
    print(True)

except ValueError:
    print(False)

Output:

Output: True

Now let's check a Date and Time string.

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "1995/12/12 12:00" # 👉️ String

# 👇 Check if my_str is DateTime
try: 
    parse(my_str, fuzzy=False)
    print(True)

except ValueError:
    print(False)

Output:

True

Another example:

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "1995-12-12 12:00am" # 👉️ String

# 👇 Check if my_str is DateTime
try: 
    parse(my_str, fuzzy=False)
    print(True)

except ValueError:
    print(False)

Output:

True

Now let's try to check a string that contains DateTime:

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "today is 1995-12-12 12:00am" # 👉️ String

# 👇 Check if my_str is DateTime
try: 
    parse(my_str, fuzzy=False)
    print(True)

except ValueError:
    print(False)

Output:

False

Oops! It is False. As I said, when you set fuzzy=False, the parser() method will check whether the string is a date, time, or both.

However, to check if the string contains DateTime, we need to set fuzzy=True.

from dateutil.parser import parse # 👉️ Import parse() module

my_str = "today is 1995-12-12 12:00am" # 👉️ String

# 👇 Check if my_str contains DateTime
try: 
    parse(my_str, fuzzy=True) # 👉️ (fuzzy=True) DateTime in String
    print(True)

except ValueError:
    print(False)

Output:

True

Check if String is Datetime using datetime()

datetime() is a module used to work with date and time. We can use the datetime() to check if a string is a specific date and time format.

The following example will check if my_str is Date with Year-Month-Day format.

from datetime import datetime # 👉️ Import datetime() module

my_str = "1995-12-12" # 👉️ String

# 👇 Check if my_str is DateTime with Year-Month-Day format
try:
    datetime.strptime(my_str, '%Y-%m-%d')
    print(True)
    
except ValueError:
    print(False)

Output:

True

%Y-%m-%d: Year-Month-Day format

Let's check another format:

from datetime import datetime # 👉️ Import datetime() module

my_str = "1995/12/12" # 👉️ String

👇 Check if my_str is DateTime with Year-Month-Day format
try:
    datetime.strptime(my_str, '%Y-%m-%d')
    print(True)
    
except ValueError:
    print(False)

Output:

False

Here we got False because my_str date format is Year/month/day.

The following example will check for Year-month-day Hours:Minutes format.

from datetime import datetime # 👉️ Import datetime() module

my_str = "1995-12-12 13:50" # 👉️ String

# 👇 Check if my_str is DateTime with the Y-m-d H:S format
try:
    datetime.strptime(my_str, '%Y-%m-%d %H:%M')
    print(True)
    
except ValueError:
    print(False)

Output:

True

Example of (a 12-hour format):

from datetime import datetime # 👉️ Import datetime() module

my_str = "1995-12-12 01:50 AM" # 👉️ String

# 👇 Check if my_str is DateTime with the Y-m-d H:S info format
try:
    datetime.strptime(my_str, '%Y-%m-%d %H:%M %p')
    print(True)

except ValueError:
    print(False)

Output:

True

We've used %H for (24-hour format) and %p for (am/pm).

Conclusion

To resume this article, we can say that when you want to check if a string is DateTime, use the parse() method. Otherwise, if you want to check if a string is DateTime with a specific format, use datetime() method.

You can find all Examples in this Video:



You'll also find the tutorial examples if you want to download them here Examples on Github.

Happy Codding!