Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Introduction
Placeholders
Numbers
Date and time
10. Formatting datetime—introduction
Padding and Alignment
Summary

Instruction

Great! Now let's see how we can format datetime variables:

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

'{:%Y-%m-%d %H:%M:%S}'.format(my_birthday_party)

Result: 2018-10-03 16:00:00

That's quite a long placeholder, but it allows us to precisely define the datetime elements we want to show as well as their exact order:

Datetime Fromat

Note that the whole placeholder must contain a colon (:) after the opening bracket, {.

Naturally, you can also change the elements to get other formatting styles:

'{:%H:%M, %d.%m.%Y}'.format(my_birthday_party)

Result: 16:00, 03.10.2018

Exercise

Use the datetime variable provided in the template, and print the following sentence:

The concert will start at 19:00, 15.06.2018!

Stuck? Here's a hint!

Use the following placeholder:

{:%H:%M, %d.%m.%Y}