Last active
November 12, 2024 03:14
-
-
Save websofter/2d6be7323a634c7740ac40cd0f129b6d to your computer and use it in GitHub Desktop.
Find Date in any text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import datefinder | |
| def find_date(text): | |
| matches = datefinder.find_dates(text, source=True) | |
| detect_dates=[] | |
| for match in matches: | |
| year = match[0].year | |
| if year > 2010 and year <= datetime.datetime.now().year: | |
| detect_dates.append(match) | |
| if len(detect_dates) > 0: | |
| return detect_dates[0][0] | |
| else: | |
| return None | |
| my_txt="""Today is 29th of May, 2020. In UK is written as 05/29/2020, or 5/29/2020 or you can even find it as 5/29/20. Instead of / you can either use - or . like 5-29-2020 or 5.29.2020. In Greece, the date format is dd/mm/yy, which means that today is 29/05/2020 and again we can use dots or hyphen between date parts like 29-05-20 or 29.05.2020. We can also add time, like 29/05/2020 19:30. Personally, my favorite date format is Y/mm/dd so, I would be happy to convert easily all these different dates to 2020/05/29""" | |
| print(find_date(my_txt)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment