Per Newgro
Per Newgro

Reputation: 93

Compare date and time value of DatetimeField and now / today

I've added a DateTimeField to my class. I would like to return True if the start_date has been reached. But my implementation is only working for the date part. Example:

Startdate = 2012-06-08 18:00:00
currentdate 2012-06-08 19:00:00 returns False
currentdate 2012-06-09 00:00:00 returns True

from django.utils.datetime_safe import datetime

class Game(models.Model):
    start_date = models.DateTimeField('date and time game is started')

    def is_started(self):
        return self.start_date <= datetime.today()

So my question is: How can i compare the date and time value of my field and the current date and time?

Upvotes: 0

Views: 1911

Answers (2)

Denis
Denis

Reputation: 7343

If we talk about convenient storage time in my opinion much better store time in ctime integers. With integers you can perform all math operations what you need without headache.

import time
time.time()

Upvotes: 0

Girish Rao
Girish Rao

Reputation: 2669

Try using datetime.now() instead of datetime.today()

class Game(models.Model):
    start_date = models.DateTimeField('date and time game is started')

    def is_started(self):
        return self.start_date <= datetime.now()

Upvotes: 2

Related Questions