Normalizing date strings with the Python dateutil module

I recently attended a Python workshop with Jeff Cohen and he answered a ton of random questions from students. One student mentioned that he was overwhelmed with the number of Python modules and Jeff told us that he has evolved his Python skills by learning at least one new module each week. I’ve started doing this as well and it’s been a HUGE help. Each Sunday I find a module I’m not familiar with in PiPY or the standard library and read through the documents. Then I do a number of coding exercises to see how the module works. Once I’m comfortable using it I try to read through the source code to see how it’s implemented under the covers. The last part is time consuming but it’s a great way to really understand how the module works.

While perusing the date modules last weekend I came across dateutil. This handy little module provides a built-in parser to take arbitrary dates and normalize them into a datetime object. If you are dealing with different data sources without a common set of formatting standards you will love this little guy! To see how this works say you have two dates and need to get the number of days between them. The following snippet does this.

>>> import dateutil.parser

>>> date1 = "2020-06-23T16:56:05Z"

>>> date2 = "June 22 2018 09:23:45"

>>> d1 = dateutil.parser.parse(date1, ignoretz=True)

>>> d2 = dateutil.parser.parse(date2, ignoretz=True)

>>> print (d1-d2).days

If you need higher resolution you can use the min, seconds, total_seconds and microseconds attributes to drill down further. This useful module made rewriting a breeze!

This article was posted by Matty on 2017-08-02 11:41:00 -0400 EDT