search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Python Datetime | Timedelta constructor

schedule Aug 10, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

A timedelta object represents the difference between two date, time, or datetime objects.

The constructor for timedelta class takes the following form:

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

All arguments are optional and default to 0.

For timedelta objects only days, seconds and microseconds are stored internally. All arguments are converted to those units:

Unit

Stored Value

1 millisecond

1000 microseconds

1 minute

60 seconds

1 hour

3600 seconds

1 week

7 days

Examples

Internal storage for timedelta object

from datetime import timedelta

delta = timedelta(
days=33,
seconds=16,
microseconds=11,
milliseconds=34000,
minutes=3,
hours=2,
weeks=3
)

# Only days, seconds, and microseconds are stored

delta
datetime.timedelta(days=61, seconds=7430, microseconds=11)

Note that days, seconds and microseconds are normalized according to the below conditions so that the representation is unique:

Normalization Conditions

0 <= microseconds < 1000000

0 <= seconds < 3600*24 (seconds in one day)

-999999999 <= days <= 999999999

Overflow Error

OverflowError occurs when the normalized value of days lies outside the above indicated range:

from datetime import timedelta

delta = timedelta(
days=999999,
seconds=16,
microseconds=11,
milliseconds=34000,
minutes=3,
hours=2,
weeks=9000000000
)

delta
OverflowError: normalized days too large to fit in a C int

Comparisons using timedelta objects

The comparisons == or != always return a bool, no matter the type of the compared object:

from datetime import timedelta

delta1 = timedelta(seconds=52)
delta2 = timedelta(hours=15, seconds=5)

print(delta2 != delta1)
print(delta2 == 3)
True
False

For all other comparisons (such as < and >), when a timedelta object is compared to an object of a different type, TypeError is raised:

print(delta2 > delta1)
print(delta2 > 3)
True
TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

Calculate number of days / minutes between two given dates

By subtracting one datetime object from another you obtain a timedelta object representing the difference between the two datetime objects:

from datetime import datetime

d0 = datetime(2019, 2, 18, 12,1,1)
d1 = datetime(2020, 10, 26,14,2,2)

delta = d1 - d0
print('Days Difference =', delta.days)

delta_hours = delta.seconds/ /3600
print('and Hours Difference =', delta_hours)
Days Difference = 616
and Hours Difference = 2

Hence we know that between the two datetime objects of d0 and d1 there is a 616 days and 2 hours difference.

NOTE

Remember from Example 1 that for timedelta objects (in this case delta) only days, seconds and microseconds are stored internally. Therefore to retrieve other units of time such as hours we must derive this by dividing seconds by 3600 (60 seconds per minute * 60 minutes per hour).

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!