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.
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 .
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.
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.
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.
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.
Assuming you are doing this within a template, you can also use the timesince template tag.
For example:
{{ blog_date|timesince:comment_date }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With