Working with Dates and Times in Python

In Cleaning and Preparing Data In Python, you worked with simple dates: the year in which an artist produced each piece of art as well as that artist’s year of birth and death. These year values were represented as integers, which made them easy to work with. Unfortunately, working with date/time data is often a lot more complex:

  • Where you have a compound date format, like January 1, 1901, separating each component value and converting it to its numeric form is cumbersome.
  • There are many different formats, e.g. 12-hour time versus 24-hour time.
  • Adding and subtracting across date/time boundaries isn’t easy — for instance, if I wanted to add 1 hour 35 minutes to the time 32 minutes, you’ll need to account for the fact that there are 60 minutes in an hour to be able to come up with the correct answer, 2 hours 7 minutess.

In this mission, we will learn how to work with dates and times in Python. Python has a module called datetime that makes working with dates and times easier. In this mission, we’ll learn how to use datetime in Python while working with a data set of visitors to the Obama White House.

Working with dates and times in Python is a vital skill because many data include date/time information, including:

  • Weather data with dates and/or times.
  • Computer logs with the timestamp for each event.
  • Sales data with date/time range included.


  • Learn to parse dates from strings using the datetime library.
  • Learn techniques for data and time analysis.
  • Learn how to format dates using strftime.

Lesson Outline

  1. Introduction
  2. Importing Modules
  3. The Datetime Module
  4. The Datetime Class
  5. Using Strptime to Parse Strings as Dates
  6. The Time Class
  7. Comparing Time Objects
  8. Calculations With Date and Time
  9. Summarizing Appointment Lengths
  10. Next Steps
  11. Takeaways

Get started for free

No credit card required.

Or With

By creating an account you agree to accept our terms of use and privacy policy.