简体   繁体   中英

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

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 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM