datetime

Converting Dates in Python

Dates are one of those annoying things that can be expressed in many different formats. For example, the date at the time of writing is 28 June 2017. I can express today's date in any number of ways, including:

  • June 28, 2017 
  • 28/06/2017
  • 28/06/17
  • 28/6/17
  • 2017-06-28

When you're working with dates in your Python projects, chances are you'll eventually need to wrangle a date in one format into another, so here's an example I came across in my own code recently.

I was parsing RSS feeds published by the British and Irish Legal Information Institute. In the feeds, dates are expressed as, for example, '28 June 2017'. For my purposes, I needed to convert the date into YYYY-mm-dd format (e.g. 2017-06-28). Here's how I dealt with it:

I was capturing the date like so:

date_in_feed = '28 June 2017'

I then set up the converter, which uses strptime, passing in the date I've captured as the first argument and it's format as the second argument:

converter = time.strptime(date_in_feed, '%d %B %Y')

Finally, to get the date the way I want, I use strpftime, passing in the desired format as the first argument and my converted (above) as the second argument.

converted_date = time.strftime('%Y-%m-%d', converter)

In the light of the myriad of alternative date structures you may be dealing with, I think you're best off looking at the datetime documentation here, but I'll add more examples as and when I come across them.