

It returned a new datetime object pointing to a past timestamp i.e. Then subtracted that to the datetime object. Then we created a relativedelta object representing 2 hours by passing hours argument as 2. We fetched the current timestamp as datetime object using datetime.now(). Past_time = current_time - relativedelta(hours=n) Subtract 2 hours to current time in python using relativedelta from datetime import datetimeįrom dateutil.relativedelta import relativedelta So, to subtract hours to a current time, we can create a relativedelta object to represents the interval in hours and then subtract it from the datetime object containing the current time. Year, Month, Day, Hours, Minutes, Seconds and Microseconds. The relativedelta class has all the attributes to represent a duration i.e. In python, the dateutil module provides a class relativedelta, which represents an interval of time. Subtract hours from current time in python using relativedelta It returned a new datetime object pointing to the past timestamp i.e. Then we subtracted this DateOffset object to the datetime object pointing to the current time. We created a DateOffset object by passing hours argument as 2. Past_time = current_time - pd.DateOffset(hours=n) Subtract 2 hours from current time in python using pandas from datetime import datetime

# Subtract 2 hours from datetime object containing current time

Subtract 2 hours from current time in python from datetime import datetime Python: Get difference between two datetimes in hours.Check If Date is Within 7 Days in Python.Python - Access Nth item in List Of Tuples Python - Check if a value is in Dictionary Python - Returning Multiple Values in Function
