-3

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 
Aran8276
  • 1
  • 2
  • substract the two times and then you have a timedelta that you can convert to hours –  May 16 '22 at 10:39
  • Don't expect the SO community to think for you. Tell us what you tried so far. – imperosol May 16 '22 at 10:54
  • I did try dateparser.parse('1 hour ago') which prints out like 2022-05-16 17:00:21.431079, but this is not what I want, I want the opposite – Aran8276 May 16 '22 at 11:01
  • [Arrow](https://arrow.readthedocs.io/en/latest/#humanize) also has humanizing functions, maybe they will be enough? – Dugnom May 16 '22 at 11:11

1 Answers1

1

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
R. Baraiya
  • 1,490
  • 1
  • 4
  • 17