3

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). Or if its just seconds left then it would be 15 seconds

  • Year is classified as 365.25 days
  • Month is classified as 30.4375 days
Zoey Malkov
  • 776
  • 6
  • 16
  • 2
    There's no built-in module to do that, so you'd probably have to implement it yourself using `timedelta.total_seconds()` and `divmod`. – AKX May 18 '20 at 06:07
  • 2
    It's going to be interesting since seconds is the only of your units that has fixed length. – Klaus D. May 18 '20 at 06:15
  • 2
    FWIW, a month is an extremely vague unit of time (and to a lesser degree year too), so giving both a pretty vague "3 months" and a hyper specific "19 seconds" is… odd… – deceze May 18 '20 at 06:22
  • @deceze updated question – Zoey Malkov May 18 '20 at 06:27
  • [This](https://stackoverflow.com/questions/4048651/python-function-to-convert-seconds-into-minutes-hours-and-days/24542445#24542445) might be a place to start – FObersteiner May 18 '20 at 11:07

0 Answers0