Python converting Time Format like DD/MM/YYYY to 5 hours ago, 3 days ago, 1 year ago, etc from strftime

182 views Asked by At

Looking for a way to convert time from strftime to ... ago, I looked everywhere on the internet and there is a way to convert ... ago to strftime, so I know it is possible but I'm not sure how to do it

input

14:56:39 PM

output

2 hours ago 
1

There are 1 answers

0
R. Baraiya On BEST ANSWER

here I tried convert timedelta, as following;

import datetime
a = datetime.datetime.now()
b = datetime.datetime(2022,5,10,12,0,0,0)
c = a - b
ts = c.total_seconds()
h = ts//3600
m = (ts%3600)//60
sec = (ts%3600)%60
print ("%d:%d hours ago" %(h,m) )

days, hours, minutes = c.days, c.seconds // 3600, c.seconds // 60 % 60
print('{}:Days {}:Hours {}:Minutes ago'.format(days, hours, minutes))


OUTPUT : 145:47 hours ago
         6:Days 1:Hours 47:Minutes ago