Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
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
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 45m
Published
Clock icon Jun 05, 2020

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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans