There are many different types of objects that we can work with in Python. Dates are one of those objects that are infamous in nearly every language. Date types are notoriously hard to wrangle but Python has quite a few ways to manipulate dates and time.

A date can be represented a number of different ways. If today were February 4th, 2018, we could potentially represent this X number of ways each being recognizable:

  • February 4th, 2018
  • Feb 04, 2018
  • 2/4/18
  • 2-4-18
  • 02/04/18
  • 4-Feb-2018

That's a lot of ways to represent the same thing! Let's see how we bend this dates to our will in Python to make the language do all of this hard work for us.

First off, to get Python to help us out manipulating how a date is formatted, we need to import the native datetime module. This module contains all of the methods we need to take care of a huge majority of the formatting needs we may have. We can import it with a simple import statement. We're using the from here so that we can reference the functions without using dot notation.

>>> from datetime import datetime

Once we do that, we now have access to a number of methods. First, let's assume we're working with some date we made up. For this example, I'm going to use 2/4/18 and to start out with I'm going to represent it as a simple string.

>>> strDate = '2/4/18'

We can't do much with a string so we need to cast this string to a datetime object so Python can understand that string is actually a date. One way to do that is to use the strptime method. This method on the datetime object allows us to pass in a date and time and use a number of different format operators to manipulate how it works.

Taking our example date, perhaps I want to change it from 2/4/18 to Feb 04, 2018. To do that, I'd pass the the original string as the first argument to the strptime() method. This method converts a string into a date object that Python can understand. The strptime() method allows you to pass in a simple string like 2/4/18 and a simple formatting string to define where the day, month and year is.

>>> objDate = datetime.strptime(strDate, '%m/%d/%y')
>>> objDate
datetime.datetime(2018, 2, 4, 0, 0)

Now that Python understands this string is an actual date, we can either leave it as-is or convert it back to a string in a different format. In this case, I'd like to convert it back to Feb 04, 2018. To do that, I can use the strftime() method that does the opposite of what we just did. This method converts a datetime object back to a string.

>>> datetime.strftime(objDate,'%b %d, %Y')
'Feb 04, 2018'

We're now back at a simple string again but this time it's been converted to a different format. How about just getting the year from date?

>>> datetime.strftime(objDate,'%Y')
'2018'

Maybe we just want the day but without the leading zero? That one require a little help from the string method lstrip() though but you get the point.

>>> datetime.strftime(objDate,'%d').lstrip('0')
'4'

Using a combination of the strptime() and strftime() methods, we can change up how a date is represented in a nearly infinite number of ways. The key is to understanding each of the formatting operators and what they represent.