Time difference in seconds,minutes, hours and days

  Uncategorized

This code is an example for calculating time difference in different units. It is a part of my one program.


import pandas as pd

DateOct02_2020=date(2020,10,2)
# ... 
data=pd.read_csv(file)
data['Date']=pd.to_datetime(data.Date)
L=data.shape[0]

for i in range(L):
    date1=data.loc[i,'Date'].date()
    ddt=DateOct02_2020-date1
    print(int(ddt.total_seconds()))  # in seconds
    print(int(ddt.total_seconds()/60))  # in minutes
    print(int(ddt.total_seconds()/60/60))  # in hours
    print(int(ddt.total_seconds()/60/60/24))  # in days