MiniGunnR
MiniGunnR

Reputation: 5800

How to get hour and minute integers from TimeField in Django?

I have a TimeField() which saves a time. I want to create a model method that finds if user has arrived after 9:00.

def late(self):
    t = strptime(self.time, "%H:%M")
    hour = int(t.tm_hour)
    min = t.tm_min
    if hour > 9:
        return True

What's wrong with this code?

Upvotes: 3

Views: 4959

Answers (2)

wpedrak
wpedrak

Reputation: 687

It's TimeField, as you said. Let's have a look at TimeField docs: A time, represented in Python by a datetime.time instance, so you shouldn't use strptime on self.time. I think that your code should looks like:

def late(self):
    return self.time.hour > 9

Same for minute

Upvotes: 3

Antoine Fontaine
Antoine Fontaine

Reputation: 802

You should not use tm_hour and tm_min but hour and minute respectively.

def late(self):
    t = strptime(self.time, "%H:%M")
    return t.hour > 9

Upvotes: 0

Related Questions