Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arithmetic Operations with timedelta Objects | Working with Dates
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

Arithmetic Operations with timedelta Objects

We can perform different arithmetic operations for multiple timedelta objects. For example, we can add, subtract, divide, find the whole part of division... What's the sense?

For example, we can calculate the whole number of weeks between two dates. Let's use the same example as before: we can calculate the whole number of weeks since the COVID-19 became a pandemic (declared by WHO). There are two ways to solve this task:

  1. Extract the number of days (by using .days) and then find the whole part of division by 7.
  2. Find the whole part of the division of two timedelta objects.
12345678910111213141516
# Load classes from library from datetime import date from datetime import timedelta # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Calculate difference between dates diff = today - pandemic # Count whole number of weeks: method 1 print(diff.days//7, "weeks passed since COVID-19 became pandemic") # Count whole number of weeks: method 2 print(diff//timedelta(weeks = 1), "weeks passed since COVID-19 became pandemic")
copy

See: in the first method, we extracted the number of days and calculated the whole part of the division by 7. In the second method, we divided by timedelta object with 1 week as an argument.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

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

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

Секція 1. Розділ 8
toggle bottom row

Arithmetic Operations with timedelta Objects

We can perform different arithmetic operations for multiple timedelta objects. For example, we can add, subtract, divide, find the whole part of division... What's the sense?

For example, we can calculate the whole number of weeks between two dates. Let's use the same example as before: we can calculate the whole number of weeks since the COVID-19 became a pandemic (declared by WHO). There are two ways to solve this task:

  1. Extract the number of days (by using .days) and then find the whole part of division by 7.
  2. Find the whole part of the division of two timedelta objects.
12345678910111213141516
# Load classes from library from datetime import date from datetime import timedelta # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Calculate difference between dates diff = today - pandemic # Count whole number of weeks: method 1 print(diff.days//7, "weeks passed since COVID-19 became pandemic") # Count whole number of weeks: method 2 print(diff//timedelta(weeks = 1), "weeks passed since COVID-19 became pandemic")
copy

See: in the first method, we extracted the number of days and calculated the whole part of the division by 7. In the second method, we divided by timedelta object with 1 week as an argument.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

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

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

Секція 1. Розділ 8
toggle bottom row

Arithmetic Operations with timedelta Objects

We can perform different arithmetic operations for multiple timedelta objects. For example, we can add, subtract, divide, find the whole part of division... What's the sense?

For example, we can calculate the whole number of weeks between two dates. Let's use the same example as before: we can calculate the whole number of weeks since the COVID-19 became a pandemic (declared by WHO). There are two ways to solve this task:

  1. Extract the number of days (by using .days) and then find the whole part of division by 7.
  2. Find the whole part of the division of two timedelta objects.
12345678910111213141516
# Load classes from library from datetime import date from datetime import timedelta # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Calculate difference between dates diff = today - pandemic # Count whole number of weeks: method 1 print(diff.days//7, "weeks passed since COVID-19 became pandemic") # Count whole number of weeks: method 2 print(diff//timedelta(weeks = 1), "weeks passed since COVID-19 became pandemic")
copy

See: in the first method, we extracted the number of days and calculated the whole part of the division by 7. In the second method, we divided by timedelta object with 1 week as an argument.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

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

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

We can perform different arithmetic operations for multiple timedelta objects. For example, we can add, subtract, divide, find the whole part of division... What's the sense?

For example, we can calculate the whole number of weeks between two dates. Let's use the same example as before: we can calculate the whole number of weeks since the COVID-19 became a pandemic (declared by WHO). There are two ways to solve this task:

  1. Extract the number of days (by using .days) and then find the whole part of division by 7.
  2. Find the whole part of the division of two timedelta objects.
12345678910111213141516
# Load classes from library from datetime import date from datetime import timedelta # Create two date objects pandemic = date(2020, 3, 11) today = date.today() # Calculate difference between dates diff = today - pandemic # Count whole number of weeks: method 1 print(diff.days//7, "weeks passed since COVID-19 became pandemic") # Count whole number of weeks: method 2 print(diff//timedelta(weeks = 1), "weeks passed since COVID-19 became pandemic")
copy

See: in the first method, we extracted the number of days and calculated the whole part of the division by 7. In the second method, we divided by timedelta object with 1 week as an argument.

Завдання

For the same two date objects as before (date1 and date2 with dates "14 January 2016" and "07 April 2019" respectively) calculate the whole number of weeks between these dates (the initial difference is saved in date_diff). Use both methods described in the example.

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