0

I have been tasked with finding the time difference between two times. The first is the "current time" as defined by the variables and the second a "deadline" also defined by variables.

Getting the code to return the variables in the time formats I want is fine, however there are two variables, one for each side. The string form of "AM"and "PM".

current_hour = 12
current_minute = 37
current_section = "PM"
due_hour = 9
due_minute = 0
due_section = "PM"

import datetime

current_time = datetime.datetime(2020,1,1) + datetime.timedelta(0,0,0,0,current_minute,current_hour)
current_time = (current_time.strftime("%I:%M" + current_section))
deadline = datetime.datetime(2020,1,1) + datetime.timedelta(0,0,0,0,due_minute,due_hour)
deadline = deadline.strftime("%I:%M" + due_section)

if current_time <= deadline:
    print(True)
else:
    current_time > deadline
    print(False)
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

Use f-strings to construct your datetime objects:

current_hour = 12
current_minute = 37
current_section = "AM"
due_hour = 12
due_minute = 0
due_section = "PM"

import datetime as dt
day = dt.date.today()

dateFormat = '%Y-%m-%d %I:%M %p'
current = dt.datetime.strptime(f'{day.year}-{day.month}-{day.day} {current_hour}:{current_minute} {current_section}', dateFormat)
deadline = dt.datetime.strptime(f'{day.year}-{day.month}-{day.day} {due_hour}:{due_minute} {due_section}', dateFormat)

print ("Current: ", current)
print ("Deadline:", deadline)
print(current > deadline)

Output:

Current:  2020-08-27 00:37:00
Deadline: 2020-08-27 12:00:00
False
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47