How to Extract Date from a String in Python?

This article will examine various techniques for obtaining dates in Python from a given string. The problem will be thoroughly explained before a variety of potential solutions are explored.

Date Extraction from a String

Problem:

We possess a string that displays the date in the format 'YYYY-MM-DD'. To extract the date from this string is our aim.

Solution:

There are various approaches that we can use in order to extract the date from the string. Some of these approaches are listed below:

  1. Using re.search() + strptime() Methods
  2. Using python-dateutil() Module
  3. Using String Manipulation
  4. Using Split and Join
  5. Using Regular Expression

Let us now discuss these methods in the following sections with the help of suitable examples.

Method 1: Using re.search() + strptime() Methods

In the following method, we will firstly check the entire string and search for the date using the search() method. After that we will use the strptime() to check if the searched string matches the required format. If so, we will print the date.

Output:

 
The org str is : I was born on 2003-10-09 in Maplewood
Computed date: 2003-10-09   

Explanation:

The code example finds a date in a text using regular expressions and uses that date to create a datetime object. The code, for instance, finds and retrieves the date and prints it in the statement.

Method 2: Using python-dateutil() Module

Output:

 
The org str is : I was born on 2003-10-09 in Maplewood 
Computed date : 2003-10-09   

Explanation:

This Python script demonstrates how to extract a date from a string using the python-dateutil module. It sets up a test string with a date in a format that is easily recognized. The date is extracted from the text using the parser.parse() method from python-dateutil after the original string has been printed. Flexible parsing is enabled by the fuzzy=True argument. Lastly, the extracted date is printed.

Method 3: Using String Manipulation

Approach:

To find the date format string in the input string, we can use string manipulation.

Algorithm:

  • Divide the input string into individual words.
  • Go over the terms one by one and see if they all fit the date format string.
  • Return the date format string if a match is discovered.

Output:

 
The org str is : I was born on 2003-10-09 in Maplewood
The Extracted Date is :
2003-10-09   

Explanation:

This program written in Python divides an input string into distinct words using the split() method, and iterates through each word. Within the loop, the program checks if the letter of the word is ten and has hyphens as the characters at positions 4 and 7, then that word indicate dates in the 'YYYY-MM-DD' format. When one of pattern is found, the loop is terminated, and the word is printed.

  • Time complexity: O(n)
  • Auxiliary Space: O(1)

Method 4: Using Split and Join

Approach:

This method divides the string into a list of words first, then separates the date from the last word. Finally, it splits the date using '-' and joins it once again using '-'.

Algorithm:

  • Divide the input string by the space character to get a list of two items: the date string "2003-10-09" and the text "I was born on"
  • Use indexing to retrieve the final item in the list, which is the date string.
  • The year, month, and day are listed when the date string is divided by the "-" character.
  • To obtain the final date string, use the join() function to combine the list's items using the "-" character.

Output:

 
The org str is : I was born on 2003-10-09
Computed date: 2003-10-09   

Explanation:

By utilizing the. split() function to split the string into words and choose the final word with [-1]; this Python script extracts a date from the end of a string. It then uses - to break this word into its component pieces and - once more to connect them. Lastly, the extracted date is printed.

  • Time complexity: O(n), where n is the string length.
  • Space complexity: O(n).

Method 5: Using Regular Expression

Approach:

Using a regular expression, the program takes the date out of a supplied text.

Algorithm:

  • Bring the re module in.
  • Specify the input text.
  • Use a regular expression pattern in conjunction with the re. findall() method to extract the date from the text.
  • Print the date that was extracted.

Output:

 
The org str is : I was born on 2003-10-09 in Maplewood
Computed date: 2003-10-09   

Explanation:

Utilizing the re module, this Python script finds a date inside a string that follows the 'YYYY-MM-DD' design. It chooses the first pattern it finds in the text, such as "2003-10-09."

  • Time Complexity: The program's temporal complexity is determined by the input string's length and the potency of the regular expression pattern, with a duration of O(n).
  • Space Complexity: The program's space complexity is O(1) due to its simple method of keeping the extracted date in a variable.