DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison
  • Real-Time Face Recognition Using OpenCV, Dlib, and Python
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Trending

  • Mobile App Development Process
  • Multi-Agent Software Engineering: One Coding Agent Isn't Enough
  • Building an AI Incident Copilot: How I Automated the First 15 Minutes of Every Production Incident
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  1. DZone
  2. Coding
  3. Languages
  4. The Datefinder Package in Python

The Datefinder Package in Python

A quick tutorial on using the datefinder package to take any string with dates in it and transform them into a list of Python datetime objects.

By 
Mike Driscoll user avatar
Mike Driscoll
·
Feb. 09, 16 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
6.6K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Python (language)

Published at DZone with permission of Mike Driscoll. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Python in 2026: uv vs Poetry vs pip: The Definitive Comparison
  • Real-Time Face Recognition Using OpenCV, Dlib, and Python
  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook