0

I am trying to compare the current time with a value given as text string. I don't know how to bring things to a common denominator, because I feel that the time obtained is not quite the same as a simple text string, even if it looks so when printing.

Test code:

import time
import datetime

def GetTimeAndWeekday(data):
  datetime_now = datetime.datetime.now()
  if data == "hms":
    time_now = datetime.time(datetime_now.hour, datetime_now.minute, datetime_now.second)
    return time_now
  elif data == "hm":
    time_now = datetime.time(datetime_now.hour, datetime_now.minute)
    return time_now
  return datetime_now

data = "hm"
found = GetTimeAndWeekday(data)
print found  # for "hm" this gives current time in HH:MM:00 format, say 05:29:00

# I want to do something like this logic:
if found == "05:29:00":
  print "the same"
else:
  print "not the same"

How can I do that logic ?

1 Answer 1

1

Use strftime()

if found.strftime('%I:%M:%S') == "05:29:00":
    print 'the same'
else:
    print 'not the same' 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.