Reputation: 7049
I have been unsuccessfully trying to calculate an expiration hour/minute for a Django model that I have. Here is the base code I am working with:
class Bribe(models.Model)
date_offered = models.DateTimeField()
def expiration(self):
[...]
return [Hours:Minutes until expiration]
My main goal is to calculate a countdown for how long a Bribe has until it will expire, in this case 48 hours. For example, if a Bribe has a date_offered
value of February 10th, 2015 @ 12:00PM
, I would like def expiration(self)
to return a string of 12 hours 10 minutes remaining until expiration
if the current date is February 11th 2015 @ 11:50PM
If anyone can help me fill in what goes inside the function/method, I would greatly appreciate it.
Upvotes: 0
Views: 367
Reputation: 45555
You can use the timeuntil()
function which is used for the timeuntil
template filter:
from django.utils.timesince import timeuntil
def expiration(self):
return timeuntil(self.date_offered)
Upvotes: 1
Reputation: 16935
I used this method to determine if my user was an adult (i.e., at least 18 years old). You should be able to manipulate it for your needs.
from datetime import date
def set_adult(self):
today = date.today()
age = today.year - self.date_of_birth.year - ((today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day))
if age >= 18:
self.adult = True
else:
self.adult = False
Upvotes: 0