Python – The datefinder package

Earlier this week, I came across another fun package called datefinder. The idea behind this package is that it can take any string with dates in it and transform them into a list of Python datetime objects. I would have loved this package at my old job where I did a lot of text file and database query parsing as there were many a time when finding the date and getting it into a format I could easily use was quite the nuisance.

Anyway, to install this handy package all you need to do is this:

pip install datefinder

I should note that when I ran this, it ended up also installing the following packages:

  • PyYAML-3.11
  • dateparser-0.3.2
  • jdatetime-1.7.2
  • python-dateutil-2.4.2
  • pytz-2015.7
  • regex-2016.1.10
  • six-1.10.0
  • umalqurra-0.2

Because of all this extra stuff, you might want to install this package into a virtualenv first. Let’s take a look at some code. Here’s a quick demo I tried:

>>> import datefinder
>>> data = '''Your appointment is on July 14th, 2016. Your bill is due 05/05/2016'''
>>> matches = datefinder.find_dates(data)
>>> for match in matches:
...     print(match)
... 
2016-07-14 00:00:00
2016-05-05 00:00:00

As you can see, it worked quite well with these two common date formats. Another format that I used to have to support was the fairly typical ISO 8601 date format. Let’s see how datefinder behaves with that.

>>> data = 'Your report is due: 2016-02-04T20:16:26+00:00'
>>> matches = datefinder.find_dates(x)
>>> for i in matches: 
...     print(i)
... 
2016-02-04 00:00:00
2016-02-04 20:16:26

Interestingly, this particular version of the ISO 8601 format causes datefinder to return two matches. The first is just the date while the second has both the date and the time. Anyway, hopefully you’ll find this package useful in your projects. Have fun!

6 thoughts on “Python – The datefinder package”

  1. Hi Mike, datefinder author here. Saw the bug with the 8601 string. I fixed the ‘double’ date result, but there is a separate bug from the underlying dateparser package that I’m still working on. That one will take me a bit longer.

  2. Hey Alec, maintainers of dateparser here. Please throw some more details about the bug and we’ll see what we can do on our side.

  3. i am trying to extract dates from strings like this:
    “(h) Commencement Date: [Section 3] Expiration Date: [Section 3] Base Rent: [Section 4(a)] May 1, 2014 May 31,2017 ”
    But it is not working. May be bcoz there is only a space between the dates. How to extract both the dates here?

Comments are closed.