Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Templates: How to show the time differences between a Datastore time and current time?

I am working on a django- Google app engine project. A user inserts some value and the datetime field is auto filled using google app engine DateTimeProperty(auto_now_add=True) property. Now, I have to display on the template the difference of this time and the current time when the user is viewing the result.

For e.g. if the inserted time was 5:00 and the user is viewing the post at 6:00, the result should be 1 hour. Is there any builtin filter available with the Django templates to perform the same? I tried timesince as:

{{ topic.creation_date| timesince: now }}

where creation_date is a datetime field. But its not working.

Please suggest.

Thanks in advance.

like image 778
Ankit Jaiswal Avatar asked Jan 23 '26 00:01

Ankit Jaiswal


1 Answers

Do you have a variable called "now" in your context? If not, that will fail.

However, you don't actually need one, because if you call the timesince filter without an argument, it will use the current time. So just do:

{{ topic.creation_date|timesince }}
like image 140
Daniel Roseman Avatar answered Jan 24 '26 22:01

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!