0

With timedelta it's possible to convert days ago to exact date:

import datetime as DT
today = DT.date.today()
week_ago = today - DT.timedelta(days=1247)
print(week_ago)

2016-10-02

Is there a function which for given utc time does the opposite?

minutes ago if delta < hour,
hours ago if delta < day,
days ago if delta < week,
weeks ago if delta < month,
years ago if delta > year

It is spossible to do this with a bunch of ifs, but not every month is equal to 30 days.

dereks
  • 544
  • 1
  • 8
  • 25
  • 1
    There's no clear definition for "a month". You may notice that `timedelta` doesn't accept `months` as a parameter and refuses to deal with them generally. "Years" has the same problem. Anything involving those intervals will need a definition by you suited for your use case, and yes, will probably involve a bunch of `if`s. – deceze Mar 02 '20 at 13:39
  • Some of the answers here seem to match what you're looking for, with seconds being parsed into a more friendly unit depending on the size of seconds. https://stackoverflow.com/questions/4048651/python-function-to-convert-seconds-into-minutes-hours-and-days – Tom Mar 02 '20 at 13:41

0 Answers0