• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Get Days of timedelta Object in Python

Get Days of timedelta Object in Python

August 19, 2022 Leave a Comment

To get the days of a timedelta object in Python, we can use the days property of a timedelta object.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.days)

#Output:
2

Another solution might be to just look at the date portions of the datetime objects before creating the timedelta object.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2.date() - datetime1.date()

print(timedelta_object.days)

#Output:
2

One last solution could be to use total_seconds and divide by 60*60*24.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds() / (60 * 60 *24))

#Output:
2.0

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to the difference between two times, it is useful to be able to easily convert to different units (seconds, minutes, hours, etc.)

We can easily convert a timedelta object in Python to days.

A timedelta object is created when you find the difference between two datetime objects in Python.

timedelta objects have many great functions, and get the days of a timedelta object, we can use the days property.

Below is a simple example of how to get the days of a timedelta object in Python.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.days)

#Output:
2

Get Seconds of timedelta Using Python

Another example which might be useful is if you wanted to get the number of seconds of a timedelta object.

To convert a timedelta to seconds in Python, we can use the total_seconds() function on a timedelta object.

Below is a simple example of how to convert a timedelta to seconds in Python.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds())

#Output:
172800.0

It might be useful to get other time periods, such as the minutes or hours of a timedelta object.

To convert a timedelta object to minutes, we can divide the call to total_seconds() by 60.

Below is an example in Python of how to convert a timedelta to minutes.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds()/60)

#Output:
2880.0

If you instead want to convert a timedelta object to hours, you can divide the call to total_seconds() by 3600.

Below is an example in Python of how to convert a timedelta to hours.

import datetime

datetime1 = datetime.datetime(2022,8,17,0,0,0)
datetime2 = datetime.datetime(2022,8,19,0,0,0)

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds()/3600)

#Output:
48.0

Hopefully this article has been useful for you to use Python to get the days of a timedelta object.

Other Articles You'll Also Like:

  • 1.  Python issuperset() Function – Check if Set is Superset of Another Set
  • 2.  Replace Forwardslashes in String Using Python
  • 3.  Using Python Selenium Webdriver to Refresh Page
  • 4.  Examples of Recursion in Python
  • 5.  Difference Between read(), readline() and readlines() in Python
  • 6.  pandas max – Find Maximum Value of Series or DataFrame
  • 7.  Remove Leading Zeros from String with lstrip() in Python
  • 8.  Calculating Sum of Squares in Python
  • 9.  PROC REG Equivalent in Python
  • 10.  Create Symbolic Link with Python os.symlink() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy