Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Utilizing Dates and Times in Python

Python is able to work with time quite efficiently, as long as the coder is aware of a few functions. In the `datetime.datetime` package: - `strptime` is used to parse a text string representing a datetime object into a datetime object. - `strftime` is used to turn a datetime object into a string representing a datetime object. - `timedelta` can be used to add to or delete a certain amount of time from a datetime object. You will need basic Python programming and datetime skills for this lab: - [Certified Associate in Python Programming Certification](https://linuxacademy.com/cp/modules/view/id/470) - [Python's datetime](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes)

Google Cloud Platform icon
Lab platform
Lab Info
Level
Beginner
Last updated
Sep 20, 2025
Duration
45m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Convert All Due Date Strings to datetime Objects

    Using the SQLite applications you have already developed, you pull all the datetimes and book titles from the database. SQLite has no datetime date type, and so we are storing them as strings. These strings need to be turned into actual datetime objects.

    Update the code so that the datetime string is converted to a datetime object, and add the last minute of the day to it:

    from datetime import datetime, timedelta
    
    # due date data from db
    title_due_dates = [
        ["Oh Python! My Python!", "2020-11-15"],
        ["Fun with Django", "2020-06-23"],
        ["When Bees Attack! The Horror!", "2020-12-10"],
        ["Martin Buber's Philosophies", "2020-07-12"],
        ["The Sun Also Orbits", "2020-10-31"]
    ]
    
    # replace the string date with a date object for each title
    # add the last minute of day to each due date
    # add a timedelta representing the last minute of the day
    for book in title_due_dates:
        due_date = datetime.strptime(book[1], "%Y-%m-%d")
        due_date = due_date + timedelta(hours=23, minutes=59)
        book[1] = due_date
    

    Congratulations! You have shown that you can convert strings to datetime objects.

  2. Challenge

    Turn All Resulting datetime Objects Into a String

    Before we can use our SQLite applications to upload the new due dates, we need to convert them to string representation.

    Run python add_time.py. This will result in an AssertionError.

    Now update add_time.py so that the datetime object is converted back to a string:

    # previous code omitted
    
    # replace the datetime object with a string representation
    # remember SQLite stores dates as strings
    for book in title_due_dates:
        book[1] = book[1].strftime("%Y-%m-%d %H:%M:%S")
    

    Run python add_time.py.

    Congratulations! You have shown that you can convert datetime objects to strings.

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight