timedelta Python Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Timedelta Python is a method used by Python that helps in calculating the difference between two dates. The Timedelta Python method is provided by the DateTime library. Timedelta Python can also be used for date manipulations in Python.

Syntax of timedelta in Python

The following is the syntax of timedelta function:

Parameters of timedelta in Python

ParameterDescriptionExample
daysvalue in days to be passed to our timedelta object. It is an optional parameter.timedelta(days=1)
secondstime value in seconds to be passed to our timedelta object. It is an optional parameter.timedelta(seconds=1)
microsecondstime value in microseconds to be passed to our timedelta object. It is an optional parameter.timedelta(microseconds=1)
millisecondstime value in milliseconds to be passed to our timedelta object. It is an optional parameter.timedelta(milliseconds=1)
minutestime value in minutes to be passed to our timedelta object. It is an optional parameter.timedelta(minutes=1)
hourstime value in hours to be passed to our timedelta object. It is an optional parameter.timedelta(hours=1)
weeksvalue in weeks to be passed to our timedelta object. It is an optional parameter.timedelta(weeks=1)

Note: All arguments are optional and default to 0. Arguments may be integers or floats and may be positive or negative.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Return value of timedelta in Python

  • timedelta object

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Examples

Timedelta function demonstration

The timedelta function in Python is used to represent a duration, which could be any number of days, seconds, and microseconds. It can easily be created and used for date manipulations.

Code

Output

Explanation The example creates a timedelta object for a duration of 5 days, 3 hours, and 30 minutes and prints it out. This demonstrates the initialization of a timedelta object.

Displaying the current date and time in Python is done using the datetime.now() function.

Code

Output

Explanation This code snippet fetches the current date and time using datetime.now() and prints it.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Calculate a date in the future

To compute a future date, add a timedelta to the current date.

Code

Output

Explanation We calculate a date 10 days from now by adding a timedelta of 10 days to the current date.

Calculate a date in the past

To find a date in the past, subtract a timedelta from the current date.

Code

Output

Explanation The example shows how to get the date 30 days ago by subtracting a timedelta of 30 days from today.

Calculate the time elapsed since a particular event or the time remaining for a particular event to occur

timedelta can be used to calculate how much time has passed since an event or how much time is left until an event occurs.

Code

Output

Explanation The first part calculates the time elapsed since New Year's Day 2024, and the second part calculates the time remaining until an event scheduled 10 days from the current date.

Subtracting a timedelta object from a date or datetime object

Subtracting a timedelta object from a date or datetime object is a straightforward task in Python, which allows you to calculate past dates or times with precision.

Example Code

Expected Output

Explanation

The code above computes the date and time one week before the current moment. This is achieved by creating a timedelta object representing 7 days and subtracting it from the current datetime. The result is a new datetime object, past_datetime, which is the exact time one week ago.

Conclusion

  • Timedelta Python helps in calculating the duration between two events.
  • Timedelta Python is a function of the Python DateTime class.
  • Timedelta Python contains the following parameters: days, seconds, microseconds, milliseconds, minutes, hours, and weeks.
  • All the parameters are optional and 0 by default.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more