15
Lesson 36 of 44

Dates and times

Learn to work with dates and times in Python. In this lesson you will use the datetime module (date, time and datetime), date arithmetic with timedelta, formatting and parsing dates with strftime, strptime and the ISO format, the calendar module and weekdays, and time zones with their best practices, and apply it to computing differences between dates, ages, working days, durations and event schedules.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Dates and times: date, time and datetime

Almost all real applications work with dates: orders, bookings, birthdays, logs. They look simple, but they have traps (months of…

Theory
Read more

Date arithmetic with timedelta

Adding 30 days to a date seems easy until you have to think about months of 28, 30 and 31 days and leap years. timedelta does all…

Theory
Read more

Formatting and parsing dates

Dates inside the program are objects; outside (on screen, in files, in forms) they are text . Converting between both is a daily…

Theory
Read more

The calendar module and weekdays

Many date problems need calendar data: how many days a month has, whether a year is a leap year, which day of the week a date…

Theory
Read more

Time zones and best practices

When an application is used in several countries, "10:00" does not mean the same to everyone. Time zones are the most common…

Theory
Read more

Practice exercises 10

Days between two dates

Create a Python program that computes how many days there are between two dates using the datetime module. Subtracting two dates…

Exercise Easy
Solve it

Day of the week

Create a Python program that tells which day of the week each date falls on. The weekday() method returns a number: 0 for Monday…

Exercise Easy
Solve it

Add days to a date

Create a Python program that adds (or subtracts) a number of days to a date using timedelta , and says which day of the week the…

Exercise Medium
Solve it

Leap years and days in a month

Create a Python program that tells how many days a month has and whether the year is a leap year , using the calendar module.

Exercise Medium
Solve it

Compute the age

Create a Python program that computes a person's age on a given date and how many days are left until their next birthday. The…

Exercise Medium
Solve it

Format dates

Create a Python program that shows each date in two readable formats . The strftime method converts a date into text following a…

Exercise Medium
Solve it

Dates with a custom format

Create a Python program that parses dates written as day/month/year and converts them to the standard ISO format, detecting the…

Exercise Medium
Solve it

Duration between two moments

Create a Python program that computes the duration between two instants (date and time) and shows it in days, hours and minutes…

Exercise Hard
Solve it

Working days

Create a Python program that counts the working days (Monday to Friday) between two dates, both included, going through the days…

Exercise Hard
Solve it

Event schedule

Create a Python program that sorts events by date and time and computes the time elapsed between the first and the last. datetime…

Exercise Hard
Solve it