avatar Bite 92. Humanize a datetime

In this Bite you will convert a timedelta object into something readable. We know: there are modules to do it, the goal though is to try to solve it yourself using Python.

Here are the requirements as layed out in the tests:

  (NOW - timedelta(seconds=2), 'just now'),
  (NOW - timedelta(seconds=9), 'just now'),
  (NOW - timedelta(seconds=10), '10 seconds ago'),
  (NOW - timedelta(seconds=59), '59 seconds ago'),
  (NOW - timedelta(minutes=1), 'a minute ago'),
  (NOW - timedelta(minutes=1, seconds=40), 'a minute ago'),
  (NOW - timedelta(minutes=2), '2 minutes ago'),
  (NOW - timedelta(minutes=59), '59 minutes ago'),
  (NOW - timedelta(hours=1), 'an hour ago'),
  (NOW - timedelta(hours=2), '2 hours ago'),
  (NOW - timedelta(hours=23), '23 hours ago'),
  (NOW - timedelta(hours=24), 'yesterday'),
  (NOW - timedelta(hours=47), 'yesterday'),
  (NOW - timedelta(days=1), 'yesterday'),
  (NOW - timedelta(days=2), '05/19/18'),
  (NOW - timedelta(days=7), '05/14/18'),
  (NOW - timedelta(days=100), '02/10/18'),
  (NOW - timedelta(days=365), '05/21/17')

So basically print a readable format up until yesterday, then just print the date in US format (%m/%d/%y). If a non datetime object is given or it is in the future, raise a ValueError.

Good luck and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #182 to crack this Bite?
Resolution time: ~57 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 5.44 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help