Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python timedelta

by suresh

The Python timedelta function is available in the datetime library. We use this Python timedelta function to calculate the difference between two dates. Or, you can use this to predict the past dates and future dates.

In this section, we show the maximum number of examples of this Python timedelta. The syntax of this Python timedelta is

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

Python timedelta Examples

It is a basic example to explain to you how the datetime timedelta works.

# Python timedelta Example
import datetime
 
print("Microseconds = ", datetime.timedelta(microseconds = 1))
print("Milliseconds = ", datetime.timedelta(milliseconds = 1))
print("Seconds      = ", datetime.timedelta(seconds = 1))
print("Minutes      = ", datetime.timedelta(minutes = 1))
print("Hours        = ", datetime.timedelta(hours = 1))
print("Days         = ", datetime.timedelta(days = 1))
print("Weeks        = ", datetime.timedelta(weeks = 1))
Python timedelta 1

timedelta attributes

The datetime timedelta object has three special attributes min, max, resolution to return the most negative, most positive, and the smallest difference between two non-equal timedelta objects.

import datetime
 
print("Minimum = ", datetime.timedelta.min)
print("Maximum = ", datetime.timedelta.max)
print("Resoultion = ", datetime.timedelta.resolution)
Python timedelta 2

Python timedelta Future Dates

Let me give you a simple Python example to return the dates one year from now and two years from now. We can say something like predicting future dates using this timedelta in datetime. 

# Python timedelta Example
 
from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_after_one_year = current_dt + timedelta(days = 365)
print("Date and Time After One Year : ", str(dt_after_one_year))
 
dt_after_two_years = current_dt + timedelta(days = 730)
print("Date and Time of Two Years from Now : ", str(dt_after_two_years))
Python timedelta 3

Python timedelta Past Dates

Here, we used the subtraction to find the past dates. From the below timedelta code, days = 730 means subtracting 730 days from the current date.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_years = current_dt - timedelta(days = 730)
print("Date and Time before Two Years from Now : ", str(dt_before_two_years))
 
dt_before_six_years = current_dt - timedelta(days = 2190)
print("Date and Time before Six Years from Now : ", str(dt_before_six_years))
 
dt_before_thirty_years = current_dt - timedelta(days = 10950)
print("Date and Time before Thirty Years       : ", str(dt_before_thirty_years))
Python timedelta 4

Python timedelta Week

We are using the weeks argument inside the timedelta function to find the date before and after 2 weeks / 4 weeks from now. 

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_weeks = current_dt - timedelta(weeks = 2)
print("DateTime before Two Weeks from Now : ", str(dt_before_two_weeks))
 
dt_after_two_weeks = current_dt + timedelta(weeks = 2)
print("DateTime after Two Weeks from Now  : ", str(dt_after_two_weeks))
 
print()
dt_before_four_weeks = current_dt - timedelta(weeks = 4)
print("DateTime before Four Weeks from Now : ", str(dt_before_four_weeks))
 
dt_after_six_weeks = current_dt + timedelta(weeks = 6)
print("DateTime after Six Weeks from Now  : ", str(dt_after_six_weeks))
Python timedelta 5

Python timedelta Days

We used the days argument inside the timedelta function to return the date before and after 2 days from now. 

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_days = current_dt - timedelta(days = 2)
print("Date and Time before Two Days from Now : ", str(dt_before_two_days))
 
dt_after_two_days = current_dt + timedelta(days = 2)
print("Date and Time of Two Days from Now     : ", str(dt_after_two_days))
Python timedelta 6

Here, we are returning the date (not Date and Time) before and after two days from now. 

from datetime import datetime, timedelta
 
dt = datetime.now()
 
current_dt = dt.date()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_days = current_dt - timedelta(days = 2)
print("Date and Time before Two Days from Now : ", str(dt_before_two_days))
 
dt_after_five_days = current_dt + timedelta(days = 2)
print("Date and Time of Five Days from Now    : ", str(dt_after_five_days))
Python timedelta 7

Python timedelta Hours

It returns the date and time before and after two hours from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_hours = current_dt - timedelta(hours = 2)
print("Date and Time before Two Hours from Now : ", str(dt_before_two_hours))
 
dt_after_two_hours = current_dt + timedelta(hours = 2)
print("Date and Time of Two Hours from Now     : ", str(dt_after_two_hours))
Python timedelta 8

timedelta Minutes

Using timedelta to return DateTime before two minutes, and after 6 minutes from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_two_minutes = current_dt - timedelta(minutes = 2)
print("Date and Time before Two Minutes from Now : ", str(dt_before_two_minutes))
 
dt_after_six_minutes = current_dt + timedelta(minutes = 6)
print("Date and Time After Six Minutes from Now  : ", str(dt_after_six_minutes))
Python timedelta 9

timedelta Seconds

In this example, we are using timedelta to return DateTime before twenty seconds, and after 90 seconds from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_twenty_seconds = current_dt - timedelta(seconds = 20)
print("DateTime before Twenty seconds : ", str(dt_before_twenty_seconds))
 
dt_after_ninty_seconds = current_dt + timedelta(seconds = 90)
print("DateTime After Ninty seconds  : ", str(dt_after_ninty_seconds))
Python timedelta 10

timedelta Milliseconds

This example prints date and time before 10000 milliseconds and after 9000000 milliseconds from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_mseconds = current_dt - timedelta(milliseconds = 10000)
print("DateTime before Ten Thousand Milliseconds : ", str(dt_before_mseconds))
 
dt_after_mseconds = current_dt + timedelta(milliseconds = 900000)
print("DateTime After 900000 Milliseconds  : ", str(dt_after_mseconds))
Python timedelta 11

Python timedelta Multiple Arguments

Until now, we are using the timedelta with a single argument. Here, we use more than one argument. current_dt – timedelta(days = 2, hours = 2) returns date and time before 2 days and 2 hours from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_days = current_dt - timedelta(days = 2, hours = 2)
print("Before Date and Time  : ", str(dt_before_days))
 
dt_after_days = current_dt + timedelta(days = 3, hours = 4)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_before_days = current_dt - timedelta(days = 2, hours = 2, minutes = 10)
print("Before Date and Time  : ", str(dt_before_days))
 
dt_after_days = current_dt + timedelta(days = 3, hours = 4, seconds = 20)
print("After  Date and Time  : ", str(dt_after_days))
Python timedelta 12

If you know the order of the arguments and you are using them in the same order, then you don’t need the name. I mean, you can place argument values. current_dt – timedelta(2, 2) returns date and time before 2 days and 2 seconds from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_days = current_dt - timedelta(2, 2)
print("Before Date and Time  : ", str(dt_before_days))
 
dt_before_days = current_dt - timedelta(2, 2, 2)
print("Before Date and Time  : ", str(dt_before_days))
 
dt_before_days = current_dt - timedelta(2, 2, 0, 2, 0, 2)
print("Before Date and Time  : ", str(dt_before_days))
 
dt_before_days = current_dt - timedelta(2, 2, 0, 2, 0, 2, 1)
print("Before Date and Time  : ", str(dt_before_days))
Python timedelta 13

Here, we are using the timedelta with argument values to predict future dates.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_after_days = current_dt + timedelta(3, 3)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(3, 3, 3, 3)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(3, 3, 0, 0, 3, 3)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(3, 0, 0, 4, 2, 3, 1)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(3, 0, 0, 0, 0, 0, 5)
print("After  Date and Time  : ", str(dt_after_days))
Python timedelta 14

Python timedelta Negative Values

Until now, we are subtracted the current date and time with timedelta values. However, you can use negative values as the timedelta arguments. For instance, current_dt + timedelta(days = -35) returns date before 35 days from now.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_after_days = current_dt + timedelta(days = -35)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(days = -3, hours = -4)
print("After  Date and Time  : ", str(dt_after_days))
 
dt_after_days = current_dt + timedelta(days = -10, hours = -40, minutes = -10)
print("After  Date and Time  : ", str(dt_after_days))
Python timedelta 15

Find total Seconds using timedelta

It prints the total number of seconds in an Hour, Day, Year, and Week

from datetime import datetime, timedelta
 
dt = timedelta(hours = 1)
print("One Hour                  = ", dt)
print("Total Seconds in One Hour = ", dt.total_seconds())
 
dt = timedelta(days = 1)
print("One Day                   = ", dt)
print("Total Seconds in One day  = ", dt.total_seconds())
 
dt = timedelta(days = 365)
print("Total Days                = ", dt)
print("Total Seconds in a Year   = ", dt.total_seconds())
 
dt = timedelta(weeks = 1)
print("One Week                  = ", dt)
print("Total Seconds in a Week   = ", dt.total_seconds())
Python timedelta 16

Python timedelta strftime

Using the timedelta function to predict future dates. Next, we are using the strftime function to format the date and times.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_after = current_dt + timedelta(days = 30)
print("Formatted DateTime  : ", dt_after.strftime('%Y/%M/%d %H:%M:%S %p'))
 
dt_after = current_dt + timedelta(days = 365)
print("Formatted DateTime  : ", dt_after.strftime('%A, %d %b %Y'))
 
dt_after = current_dt + timedelta(days = 365)
print("Formatted DateTime  : ", dt_after.strftime('%A, %d %b %Y %H:%M:%S %p'))
Python timedelta 17

Python Time difference

Finding the time difference between the current date and the date returned by the timedelta.

from datetime import datetime, timedelta
 
current_dt = datetime.now()
print("Current Date and Time : ", str(current_dt))
 
dt_before_ten_days = current_dt - timedelta(days = 10)
print("DateTime before Ten Days from Now : ", str(dt_before_ten_days))
print("Time Difference = ", current_dt - dt_before_ten_days)
 
dt_after_two_days = current_dt + timedelta(days = 2)
print("DateTime of Two Days from Now     : ", str(dt_after_two_days))
print("Time Difference = ", current_dt - dt_after_two_days)
Python timedelta 18

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy