pythonpython-dateutil

Getting exception : Unknown string format:


I am trying to extract dates from text using dateutil library (Python 3.7)

I want to extract all dates from text using code below.

import dateutil.parser as dparser

text = 'First date is 10 JANUARY 2000 and second date is 31/3/2000'

dt = dparser.parse(text, fuzzy=True, dayfirst=True, default=datetime.datetime(1800, 1, 1)) 

But getting following exception

Unknown string format: First date is 10 JANUARY 2000 and second date is 31/1/2000

Please let me know any way to extract multiple dates in the text.


Solution

  • How about using datefinder?

    import datefinder
    
    string_with_dates = '''
        First date is 10 JANUARY 2000 and second date is 31/3/2000
    '''
    
    matches = datefinder.find_dates(string_with_dates)
    for match in matches:
        print(match)
    
    Output:
    2000-01-10 00:00:00
    2000-03-31 00:00:00