Joel
Joel

Reputation: 673

One hour difference in Python

I have a datetime.datetime property var. I would like to know if it is less than one hour of the current time. Something like

var.hour<datetime.datetime.today().hour - 1

Problem with the above syntax is that

datetime.datetime.today().hour

returns a number such as "10" and it is not really a date comparation but more of a numbers comparation.

What is the correct syntax?

Thanks!

Joel

Upvotes: 11

Views: 23032

Answers (2)

pocoa
pocoa

Reputation: 4337

You can use dateutil.relativedelta

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

now = datetime.now()
other_time = now + timedelta(hours=8)
diff = relativedelta(other_time, now)
print diff.hours # 8

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599540

Use datetime.timedelta.

var < datetime.datetime.today() - datetime.timedelta(hours=1)

Upvotes: 16

Related Questions