如何在没有外部库的情况下获得人性化的时差

I have two times, how can I get the time difference of these 2 aware datetime objects in a human readable format. What I mean by human readable format would be 1 year 3 months 2 weeks 4 days 1 hour 2 minutes and 19 seconds.

However, if the time difference is shorter, it would give a readable format like 2 minutes and 3 seconds (It wouldn't tell us 0 years 0 months 0 weeks 0 days 0 hours 2 minutes and 52 seconds) without a external library. I know there is a library called humanfriendly.

timezone = timezone('Australia/Sydney')

startDate = datetime.now(timezone)

dateStr = '2021-03-23 13:03:12'
endTime = datetime.strptime(dateStr, '%Y-%m-%d %H:%M:%S')
endTime = endTime.replace(tzinfo=timezone)
endDate = endTime + timedelta(minutes=1)

timeDiff = endDate - startDate
评论