Date Formatting using Python Datetime and Strptime

Date Formatting using Python Datetime and Strptime

Posted on February 23, 2018 0 Comments

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 date! Let's see how we bend these dates to our will in Python to make the language do all of this hard work for us.

Python Module for Handling Dates

To get Python to manipulate 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 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.

 

Converting Strings into Dates

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)

Converting Dates into Strings

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 requires a little help from the string method lstrip().

>>> 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.

Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation