Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Date and Time Difference | Working with Times
Dealing with Dates and Times in Python
course content

Зміст курсу

Dealing with Dates and Times in Python

Dealing with Dates and Times in Python

1. Working with Dates
2. Working with Times
3. Timezones and Daylight Savings Time (DST)
4. Working with Dates and Times in pandas

Date and Time Difference

Remember the timedelta object we learned in the first section? At that time we missed the time part in timedelta objects since we compared two dates only. Now there is even more sense in using timedelta since we can perform different operations with time also.

For example, imagine that we have both departure and arrival times on the ticket. Using timedelta we can easily calculate the expected duration for this trip.

  • Departure: 25 May 2021, 18:20
  • Arrive: 26 May 2021, 07:40
12345678910
# Load classes from library from datetime import datetime from datetime import timedelta # Create two datetimes objects dep = datetime(2021, 5, 25, 18, 20) arr = datetime(2021, 5, 26, 7, 40) # Calculate estimated trip duration print(f"Estimated trip duration: {arr - dep}")
copy

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 2. Розділ 7
toggle bottom row

Date and Time Difference

Remember the timedelta object we learned in the first section? At that time we missed the time part in timedelta objects since we compared two dates only. Now there is even more sense in using timedelta since we can perform different operations with time also.

For example, imagine that we have both departure and arrival times on the ticket. Using timedelta we can easily calculate the expected duration for this trip.

  • Departure: 25 May 2021, 18:20
  • Arrive: 26 May 2021, 07:40
12345678910
# Load classes from library from datetime import datetime from datetime import timedelta # Create two datetimes objects dep = datetime(2021, 5, 25, 18, 20) arr = datetime(2021, 5, 26, 7, 40) # Calculate estimated trip duration print(f"Estimated trip duration: {arr - dep}")
copy

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 2. Розділ 7
toggle bottom row

Date and Time Difference

Remember the timedelta object we learned in the first section? At that time we missed the time part in timedelta objects since we compared two dates only. Now there is even more sense in using timedelta since we can perform different operations with time also.

For example, imagine that we have both departure and arrival times on the ticket. Using timedelta we can easily calculate the expected duration for this trip.

  • Departure: 25 May 2021, 18:20
  • Arrive: 26 May 2021, 07:40
12345678910
# Load classes from library from datetime import datetime from datetime import timedelta # Create two datetimes objects dep = datetime(2021, 5, 25, 18, 20) arr = datetime(2021, 5, 26, 7, 40) # Calculate estimated trip duration print(f"Estimated trip duration: {arr - dep}")
copy

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Remember the timedelta object we learned in the first section? At that time we missed the time part in timedelta objects since we compared two dates only. Now there is even more sense in using timedelta since we can perform different operations with time also.

For example, imagine that we have both departure and arrival times on the ticket. Using timedelta we can easily calculate the expected duration for this trip.

  • Departure: 25 May 2021, 18:20
  • Arrive: 26 May 2021, 07:40
12345678910
# Load classes from library from datetime import datetime from datetime import timedelta # Create two datetimes objects dep = datetime(2021, 5, 25, 18, 20) arr = datetime(2021, 5, 26, 7, 40) # Calculate estimated trip duration print(f"Estimated trip duration: {arr - dep}")
copy

Завдання

Sydney - Perth is one of the world's longest direct rail route, running for 4,352 kilometers or 2,704 miles. The ticket information looks as follows:

  • Departure: July 19, 03:00 PM
  • Arrival: July 23, 03:00 PM.

The year on the tickets is 2022. Create variables dep and arr with the respective dates and times and calculate the duration of such a trip.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 7
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt