0

I have this issue with date time, I don't understand the datetime module and django dates either, what I am trying to achieve is get the difference days between a constant datetime saved in the database and current date. See, the problem is even though today is not over I am getting negative -1 day and the day is not over yet.

from django.utils.timezone import now

class ExamplModel(models.Model):
    due_date = models.DateTimeField()


    # model method
    def get_due_date(self):
        days_left = due_date - now()
        return days_left

if due_date is today a couple of hours ago, example I get result like this -1 day, 18:54:04.590519 instead of 0 day, 18:54:04.590519

How can I solve this.

1 Answers1

0

Try this...

Gives a UTC offset, but also takes daylight savings into account.

 import time
 offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
 offset / 60 / 60 * -1
-9

The value of time.timezone or time.altzone is in seconds West of UTC (with areas East of UTC getting a negative value). This is the opposite to how we'd actually like it, hence the * -1.

from... Get time zone information of the system in Python?