Danny Watson
Danny Watson

Reputation: 165

Python - Is current time within two datetime values

I've looked around here for a while trying all different kinds of accepted answers, but they are too irrelevant for my own needs and 90% require static typing of the current date which is something I don't want to do

Context

Quite simple.

Get the current time in Spain.

Have a variable already set up which is an agreed start time.

Have a variable already set up which is an agreed end time.

If the current time in Spain is between the start-end time, proceed with saving logs

Threads checked for solution to my problem

All useless, downvoted, or irrelevant due to static typing of datetime

Python current time

Compare dates Python

Convert string into datetime

Compare date and datetime

Check if time is between two days

Convert string into datetime

Compare if datetime.timedelta is between two values

Code so far

now_time_uk = datetime.datetime.now()
current_time_spain = now_time + datetime.timedelta(hours=1)
start = datetime.time(10, 50)
end = datetime.time(16, 00)

if current_time_spain >= start or current_time_spain <= end:
    print('Start logging')
else:
    print('No need to log')

The code above was extracted from a thread on S.O which was an accepted answer, when I ran this, it gives a TypeError

TypeError: can't compare datetime.datetime to datetime.time

Accepted Answer Code

now_time_uk = datetime.datetime.now()
print(now_time_uk)
current_time_spain = now_time_uk + datetime.timedelta(hours=1)
start = datetime.time(10, 50)
end = datetime.time(17, 00)
print(current_time_spain.time())

if current_time_spain.time() >= start and current_time_spain.time() <= end:
    print('Start logging')
else:
    print('No need to log')

Upvotes: 2

Views: 152

Answers (3)

S. Galati
S. Galati

Reputation: 58

You can’t compare a date time within a time but you could continue to use a TimeDelta in order to create a start and end date to compare. With this solution your code could become:

now_time_uk = date time.datetime.now()
current_time_spain = now_time + datetime.timedelta(hours=1)
start_hours = 10
start_minutes = 50
end_hours = 16
end_minutes = 0
time_spain_start = current_time_spain + datetime.timedelta(hours=start_hours, minutes=start_minutes)
time_spain_end = current_time_spain + datetime.timedelta(hours=end_hours, minutes=end_minutes)

if current_time_spain >= time_spain_start or current_time_spain <= spain_time_end:
    print “Start logging”
else:
    print “No need to log”

In this way you can also add a variable for each parameter of the TimeDelta object in order to have different time for the end date even if the day is the next or whatever you want or need. We can imagine to have all variable days, hours, minutes, and seconds initialized to the 0 value, so you need to change only the value you need.

Upvotes: 0

Ofer Sadan
Ofer Sadan

Reputation: 11922

Like the error says you are trying to compare datetime objects with time objects which isn't possible... luckily the datetime object has a builtin method for converting to a time object, and that is .time(). So you can replace:

if current_time_spain >= start or current_time_spain <= end:

With

if current_time_spain.time() >= start or current_time_spain.time() <= end:

And it should work. Another way of doing it is setting current_time_spain from the beginning as a time object or another option is to set the start and end times to be datetime objects instead of time

Upvotes: 2

Umair Mohammad
Umair Mohammad

Reputation: 4635

You're comparing datetime object with date & time with only time , as mentioned in error

datetime.datetime.now() this returns datetime whereas datetime.time(10, 50) returns time.

If you want to only compare time then why you don't simply compare hours and minutes from now()

Upvotes: 1

Related Questions