Home > Software design >  Can you convert a Django 'DateField()' to 'date'?
Can you convert a Django 'DateField()' to 'date'?

Time:01-22

I was using the 'DateField()' in Django, and I wanted to find the time between two dates that are stored in the 'DateField()' field. How can I do this?

This is what I was trying originally: length = datetime.timedelta(start, end)

It gives me this error: TypeError: unsupported type for timedelta seconds component: DateField

CodePudding user response:

The Django DateField represents dates as datetime.date objects. You can get the timedelta by using regular arithmetic operators: difference = end-start. That will give you a timedelta object, and you can access the days member of that to see how many days are between start and end.

CodePudding user response:

You can just use a django filter {{datetime|data:"d F Y" }} on your template. That code return dd/mm/yyyy date format.

  • Related