Python - How to calculate elapsed time from date X?

I have a database table with articles, and each of these articles has a submission date. I need to calculate the days and hours since the article was published in a database, for example:

This article has been published 4 hours ago.
This article has been published 3 days and 4 hours ago.

Already have some code that I can reuse for this? I searched on Google, but maybe I am not using the right words.

Any clue that can help me?

Regards,

+5
source share
2 answers

Look at the datetimepackage , it has everything you need.

, timedelta. total_seconds(), , . - .

+9

datetime, - https://gist.github.com/207624, datetime .

+4

All Articles