Answer the question
In order to leave comments, you need to log in
How to add string date to current date?
from django.utils.timezone import now
print(now(), obj.freeze_time, sep="|||")
# вывод: 2017-09-27 15:26:40.445545+00:00|||1:58:29
now() + obj.freeze_time
unsupported operand type(s) for +: 'datetime.datetime' and 'str'
class General(models.Model):
...
start_date = models.DateTimeField()
end_date = models.DateTimeField()
freeze = models.BooleanField(default=False)
freeze_time = models.CharField(max_length=200, null=True, blank=True)
Answer the question
In order to leave comments, you need to log in
Use the freeze_time
number of seconds for storage, it will be more convenient to store in the database and it is conveniently converted to / from timedelta , which can already be added to datetime
.
Example:
>>> from datetime import datetime, timedelta, now
>>> begin_date = datetime(2017, 9, 1, 0, 0, 0)
>>> end_date = datetime(2017, 9, 30, 0, 0, 0)
>>> freeze_time = end_date - now()
>>> freeze_time
datetime.timedelta(2, 17264, 411894)
>>> freeze_time.total_seconds()
190064.411894
>>> freeze_time = int(freeze_time.total_seconds())
>>> freeze_time
190064
>>> now() + timedelta(seconds=freeze_time)
datetime.datetime(2017, 9, 30, 0, 1, 1, 371159)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question