I am trying to compare two datetime times in a while loop in python and when they are equal I want to break out of the loop
I have created a while loop and in it I have an if statement that compares the two datetime times together. The problem is that the if statement doesn't execute
d=datetime.datetime.strptime('01:26:00','%H:%M:%S')
dnow=datetime.datetime.now()
while True:
time.sleep(1)
if(dnow.time() != d.time()):
print(datetime.datetime.now())
else:
print('Hello World')
break
I am expecting when dnow.time() equals d.time() then the else statement gets executed. Print out of the program 2019-07-30 01:25:54.422644 2019-07-30 01:25:55.423967 2019-07-30 01:25:56.425256 2019-07-30 01:25:57.426535 2019-07-30 01:25:58.427819 2019-07-30 01:25:59.429103 2019-07-30 01:26:00.429910 2019-07-30 01:26:01.431201 2019-07-30 01:26:02.432484 2019-07-30 01:26:03.434393 2019-07-30 01:26:04.434830
>=or<=instead of==or!=dnowand just checkif (datetime.datetime.now() >= d)>=. And displaydanddnowto see what you have. maybe you have different dates - ie.01.01.1970?