Reputation: 7371
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.
Upvotes: 31
Views: 37709
Reputation: 2015
Simply we can add the custom property to calculate the time difference with the help @property
decorator in that model.
from django.utils import timezone
class Post(models.Model):
time_posted = models.DateTimeField(auto_now_add=True, blank=True)
content = models.TextField()
@property
def time_diff(self):
return timezone.now() - self.time_posted
time_diff
will return object of datetime.timedelta
post = Post.objects.get(pk=1) # Post model object
# time diff in seconds.
post.time_diff.seconds
>>> 652
# time diff in days.
post.time_diff.days
>>> 0
Already answered above nicely by Martijn Pieters, just adding @property
, and django.utils.timezone
to calculate the difference with respective timezone from settings.py
Upvotes: 0
Reputation: 366
Just in case you want to put this process in you Django signals. Here's the one that is working for me. Hope this helps!
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import YourModel
from datetime import datetime
@receiver(pre_save, sender = YourModel)
def beforeSave(sender, instance, **kwargs):
date_format = "%H:%M:%S"
# Getting the instances in your model.
time_start = str(instance.time_start)
time_end = str(instance.time_end)
# Now to get the time difference.
diff = datetime.strptime(time_end, date_format) - datetime.strptime(time_start, date_format)
# Get the time in hours i.e. 9.60, 8.5
result = diff.seconds / 3600;
Upvotes: 0
Reputation: 1121266
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.
Upvotes: 43
Reputation: 80011
Assuming you are doing this within a template, you can also use the timesince template tag.
For example:
{{ blog_date|timesince:comment_date }}
Upvotes: 12
Reputation: 53316
Your code
timediff = datetime.datetime.now() - self.pub_date
should work to get the time difference. However, this returns timedelta
object. To get difference in seconds you use .seconds
attribute
timediff = datetime.datetime.now() - self.pub_date
timediff.seconds # difference in seconds.
Upvotes: 1