27

This is probably a very basic question but after reading documentation I still can't figure out how to do it...

I have two strings in Python that contain dates of unknown format. I don't know what formats they are in, except I know that both are valid date-time expressions. For example, one of them might be in the ISO format and the other in some other format.

All I need is to be able to compare the dates. What's the correct way to turn strings into appropriate date-time objects so that they can be compared?

thanks!

I Z
  • 5,719
  • 19
  • 53
  • 100
  • 2
    I updated the title; the issue isn't "comparing datetime strings", it is "converting the strings to a comparable value". –  Nov 06 '12 at 20:10

1 Answers1

38

The dateutil module has a date parser which can parse date strings in many formats.

For example,

In [13]: import dateutil.parser as parser

In [14]: parser.parse("19970902T090000")
Out[14]: datetime.datetime(1997, 9, 2, 9, 0)

In [15]: import datetime as dt

In [16]: now = dt.datetime.now()

In [17]: now.isoformat()
Out[18]: '2012-11-06T15:08:51.393631'

In [19]: parser.parse('2012-11-06T15:08:51.393631')
Out[19]: datetime.datetime(2012, 11, 6, 15, 8, 51, 393631)

In [20]: parser.parse('November 6, 2012')
Out[20]: datetime.datetime(2012, 11, 6, 0, 0)

Note that some datetime strings can be ambiguous: 10-09-2003 could mean October 9 or September 10, for example. dateutil has parameters like dayfirst and yearfirst to handle this:

In [21]: parser.parse("10-09-2003")
Out[21]: datetime.datetime(2003, 10, 9, 0, 0)

In [22]: parser.parse("10-09-2003", dayfirst = True)
Out[22]: datetime.datetime(2003, 9, 10, 0, 0)

In [23]: parser.parse("10-09-03", yearfirst = True)
Out[23]: datetime.datetime(2010, 9, 3, 0, 0)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677