python - How To Calculate days Between Two Dates In Django -
i want calculate days difference between 2 dates in django. trying code:
my model :
datededebut = models.datetimefield(auto_now_add=false, auto_now=false) datedefin = models.datetimefield(auto_now_add=false, auto_now=false)
my classviewset :
class myclassviewset(viewsets.modelviewset): serializer_class = myclassserializer permission_classes = (isauthenticated, ) def perform_create(self, serializer): if (condition): diff = (self.request.data.get('datedefin') - self.request.data.get('datededebut')).days #never executed print(diff)
and error :
unsupported operand type(s) -: 'unicode' , 'unicode'
this solution works me
class myclassviewset(viewsets.modelviewset): serializer_class = myclassserializer permission_classes = (isauthenticated, ) def perform_create(self, serializer): if (condition): start_date = datetime.datetime.strptime(self.request.data.get('datedefin'), "%y-%m-%d %h:%m") end_date = datetime.datetime.strptime(self.request.data.get('datededebut'), "%y-%m-%d %h:%m") diff = abs((end_date-start_date).days) print(diff)
Comments
Post a Comment