2

I have two dates:

Sat Mar 15 19:47:17 +0000 2014

2014-03-12 19:50:22.159411+00:00

I need to compare these two dates but I get the error

TypeError: can't compare datetime.datetime to unicode

How should I convert one of them?

blackmamba
  • 1,952
  • 11
  • 34
  • 59
  • You don't have two dates, you obviously have one string and one date(time), so make sure they're of the same type before you compare. – Lasse V. Karlsen Mar 15 '14 at 20:05
  • @Lasse I think the OPs aware of that - although the wording is a bit confusing. I think *date* is being used as a general term, as *unicode* is mentioned, the `TypeError` is shown and the question is: "How do I convert one of them?" – Jon Clements Mar 15 '14 at 20:06
  • Yes date is a general term here. – blackmamba Mar 15 '14 at 20:11

1 Answers1

5

The easiest method is to use the 3rd party dateutil lib, and do:

from dateutil.parser import parse as parse_date

unicode_text = 'Sat Mar 15 19:47:17 +0000 2014'
dt = parse_date(unicode_text)
# 2014-03-15 19:47:17+00:00
if dt == other_datetime:
    # do something
Jon Clements
  • 138,671
  • 33
  • 247
  • 280