Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Placeholders
Numbers
Date and time
9. Using datetime in Python
Padding and Alignment
Summary

Instruction

Well done! Let's move on to formatting date and time data.

You may not know how to use datetime variables in Python yet, but it's actually not that difficult. Take a look:

from datetime import datetime
my_birthday_party = datetime(2018, 10, 3, 16, 0, 0)

First, we need to import the datetime module, as shown on line 1. Then we can create a datetime variable using the following syntax: datetime(2018, 10, 3, 16, 0). This means:

Create a datetime variable with the following parts:
  • year: 2018
  • month: 10 (October)
  • day of month: 3
  • hour: 16 (i.e. 4 PM)
  • minutes: 0
  • seconds: 0

Note that we need to use the 24-hour clock here, so 4 PM becomes 16.

Exercise

Create a datetime variable named surgical_appointment with the following date and time:

12th December 2018, 5:30:00 PM.

Stuck? Here's a hint!

Remember to import the datetime module.

Remember that 5:30:00 PM is actually 17:30:00.