I want to calculate days difference between current date and previous date. i am trying this code
requiremntObj = CustomerLeads.objects.all()
a = datetime.datetime.now().date()
for i in requiremntObj:
date1=i.posting_date
diff = a-date1
print diff
I got a error unsupported operand type(s) for -: 'datetime.date' and 'unicode'
For current date i am getting datetime object and for date1 i am getting unicode.
posting_date = models.DateField()
If you have DateTimeField
you can use:
delta = datetime.now().date() - posting_date
print delta.days
If it is string, then you have to convert:
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime(str(datetime.now().date()), date_format)
b = datetime.strptime(str(posting_date), date_format)
delta = b - a
print delta.days
Here is post .
This code for HTML in Django
<p> {{ to_date|timeuntil:from_date }} </p>
This code for server site in python
import datetime
from_date = datetime.datetime(2019, 10, 21)
to_date = datetime.datetime(2019, 10, 25)
result = to_date - from_date
print(result.days)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.