Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to get a time difference from the time post?

Tags:

Say I have a class in model

 class Post(models.Model):      time_posted = models.DateTimeField(auto_now_add=True, blank=True)       def get_time_diff(self):          timediff = timediff = datetime.datetime.now() - self.time_posted          print timediff # this line is never executed          return timediff 

I defined a get_time_diff to get the time difference from the time when the Post is posted up to now, according to the document, the DateTimeField should be able to be converted to datetime automatically, is that correct? Why the print statement is never being run? How can you extract the time difference?

Beside, if you get a time difference, is there an easy way to convert the time difference to an integer, like the number of seconds of the total time.

like image 335
Bob Fang Avatar asked Apr 15 '13 13:04

Bob Fang


People also ask

How does Django calculate time difference?

total_seconds() returns a float value, including microseconds. Note that you need to use a timezone aware datetime object, since the Django DateTimeField handles timezone aware datetime objects as well. See Django Timezones documentation. Because both objects are timezone aware (have a .

How do I show time in Django?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value. Now, we can either assign this method to a variable or we can directly use this method wherever we required the datetime value.

What is date/time field in Django?

DateTimeField is a date and time field which stores date, represented in Python by a datetime. datetime instance. As the name suggests, this field is used to store an object of datetime created in python.

How does Django store time zones?

The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.


2 Answers

Your code is already working; a datetime.timedelta object is returned.

To get the total number of seconds instead, you need to call the .total_seconds() method on the resulting timedelta:

from django.utils.timezone import utc  def get_time_diff(self):     if self.time_posted:         now = datetime.datetime.utcnow().replace(tzinfo=utc)         timediff = now - self.time_posted         return timediff.total_seconds() 

.total_seconds() returns a float value, including microseconds.

Note that you need to use a timezone aware datetime object, since the Django DateTimeField handles timezone aware datetime objects as well. See Django Timezones documentation.

Demonstration of .total_seconds() (with naive datetime objects, but the principles are the same):

>>> import datetime >>> time_posted = datetime.datetime(2013, 3, 31, 12, 55, 10) >>> timediff = datetime.datetime.now() - time_posted >>> timediff.total_seconds() 1304529.299168 

Because both objects are timezone aware (have a .tzinfo attribute that is not None), calculations between them take care of timezones and subtracting one from the other will do the right thing when it comes to taking into account the timezones of either object.

like image 149
Martijn Pieters Avatar answered Sep 29 '22 13:09

Martijn Pieters


Assuming you are doing this within a template, you can also use the timesince template tag.

For example:

{{ blog_date|timesince:comment_date }} 
like image 26
Wolph Avatar answered Sep 29 '22 12:09

Wolph