-4

I have 2 date time objects:

dt1 = 2016-04-18 20:15:07
dt2 = 2016-04-18 20:15:07+00:00

and I want to compare these 2 which should give me true as both are technically same. but when I do :

if(dt1 == dt2):
    print("times match!)

I always get a false condition here. Any pointers on how to get over this?

codec
  • 7,978
  • 26
  • 71
  • 127
  • 4
    Your first snippet is not Python. What are `dt1` and `dt2` exactly? –  May 06 '16 at 08:51
  • You need to parse string and compare `DateTime` objects. See http://stackoverflow.com/questions/127803/how-to-parse-an-iso-8601-formatted-date-in-python – Viach Kakovskyi May 06 '16 at 08:54
  • No its not python. That's just a representation of the date time object value I have in python. The actual datetime object is getting created via an automation process. – codec May 06 '16 at 08:54

1 Answers1

3

I assume that dt1 and dt2 are string

import datetime

dt1 = "2016-04-18 20:15:07"
dt2 = "2016-04-18 20:15:07+00:00"


d1 = datetime.datetime.strptime(dt1, "%Y-%m-%d %H:%M:%S")
d2 = datetime.datetime.strptime(dt2, "%Y-%m-%d %H:%M:%S+%f:00")

print(d1)
print(d2)

if d1 == d2:
    print("times match!")
backtrack
  • 7,996
  • 5
  • 52
  • 99