Using datetime to compare with dates in Django
Solution 1
I think the problem is in the line
if datetime.now() == payment_date:
That will literally see if the payment_date
is right now. I think you want to see if now is greater than or equal to the payment_date
, in which case you should use
if datetime.now() >= payment_date:
You can also just filter the invoices when you query the database:
invoices_list = Invoice.objects.filter(payment_date__lte=datetime.now())
Update
Your code is wrong because you have mutually exclusive conditionals. Look:
if payment_date <= datetime.now():
owing = invoice_gross
if payment_date > datetime.now():
owing = 0
That first checks to see if payment_date
is before now. Then it sets owing
to invoice_gross
. Then, in the same conditional, it checks to see if payment_date
is after now. But that can't be! You are only in this block of code if payment_date
is before now!
I think you have an indentation error, and want this instead:
if payment_date <= datetime.now():
owing = invoice_gross
if payment_date > datetime.now():
owing = 0
Which, of course, is the same as:
if payment_date <= datetime.now():
owing = invoice_gross
else:
owing = 0
Solution 2
Use datetime.now()
(notice the parens). Other than that, remember that the field will always be a datetime
object. Also, (I guess that) you should check only the date of the datetime to match the current date (or else it will only match that specific second). For that you have to check if payment_date.date() == date.today()
(where date
is datetime.date
)
This also means that you can filter like this: Invoice.objects.filter(payment_date__lte=datetime.now())
.
__lte
, __gte
, __lt
, __gt
are used for <=
, >=
, <
and >
Shehzad009
Updated on January 07, 2020Comments
-
Shehzad009 over 4 years
I have a question in Django on how you can compare dates to solve some solutions. For example I have a datefield in my models.py Like below.
class Invoice(models.Model): payment_date = models.DateTimeField()
What I want to be able to do is ask if the is a way to compare a datetime.now with a DateTimeField. For example, if I had a list of payment dates and I wanted to compare with datetime now. Thhe payment_date's that are late with their payments are shown in owing. Otherwise, it the value is zero.
Here is my views to show whats going on. I have tried so far but I get a 0 value for payment_date's which are later than the payment date.
Edit here is my latest views. Funny thing is that I seem to be getting the owing = invoice_gross for all results - unlike before when I was getting all 0s. So it is still not working properly.
@login_required def homepage(request): invoices_list = Invoice.objects.all() invoice_name = invoices_list[0].client_contract_number.client_number.name invoice_gross = invoices_list[0].invoice_gross payment_date = invoices_list[0].payment_date if payment_date <= datetime.now(): owing = invoice_gross if payment_date > datetime.now(): owing = 0 return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_name':invoice_name, 'invoice_gross':invoice_gross,'payment_date':payment_date,'owing':owing}, context_instance=RequestContext(request))
Oh and my table is basically doing something like this.
ID Owing 1 100 (All the same value) 2 100 3 100 . . . . . .
-
Shehzad009 over 13 yearsif I change "if datetime.now >= payment_date:" I get this error can't compare datetime.date to builtin_function_or_method
-
crodjer over 13 years
datetime.now
is a function, call it bydatetime.now()
-
Shehzad009 over 13 yearsBack on my computer. It is still not working properly. Look at the latest code.
-
Shehzad009 over 13 yearsThe funny indent error has stopped appearing. Only problem is I still have the same problem. I have a table where in one column displays invoice number. The other displays owing payments. Each invoice number correspond to that owing. When there is least one owing = invoice_gross, ALL of the rows in that column displays the same owing value. I am not sure why it is doing this.
-
mipadi over 13 years@Shehzad009: Well, you're only checking the payment date of the first invoice, and setting
owing
to that value. Is that what you want? -
Shehzad009 over 13 years@mipadi One invoice has one payment_date. I need to check for all payment_date. If one payment_date that is overdue, their value is the same as invoice gross. If one payment_date is not overdue the value is zero. Hope that helps