Let's say I have a string: "10/12/13" and "10/15/13", how can I convert them into date objects so that I can compare the dates? For example to see which date is before or after.
Asked
Active
Viewed 1.5e+01k times
54
-
http://docs.python.org/2/library/datetime.html – TerryA Dec 04 '13 at 02:46
-
[`dateutil`](http://labix.org/python-dateutil) – gongzhitaao Dec 04 '13 at 02:46
-
1How the heck can you have a date `10/15/13`? – aIKid Dec 04 '13 at 02:49
-
8@aIKid October 15, 2013? – gongzhitaao Dec 04 '13 at 02:49
-
if this is all you need to do and these dates are always of this format, would it not be easier to compare them yourself? – WindowsMaker Dec 04 '13 at 02:50
-
@gongzhitaao ahaha. Missed that – aIKid Dec 04 '13 at 02:52
7 Answers
64
Use datetime.datetime.strptime
:
>>> from datetime import datetime as dt
>>> a = dt.strptime("10/12/13", "%m/%d/%y")
>>> b = dt.strptime("10/15/13", "%m/%d/%y")
>>> a > b
False
>>> a < b
True
>>>
-
for some reason %y (lower case y) gave me "Unconverted data remains " error, while %Y (upper case Y) worked. Why? – LazerSharks Nov 06 '14 at 22:27
-
9@Gnuey - The datetime format strings are case-sensitive. `%Y` is not the same as `%y`. `%Y` tells Python to match a 4-digit year such as `2014`. `%y` however matches a 2-digit year such as `14` for the year `2014`. You must be trying to match a 4-digit year with the 2-digit `%y` specifier. Here is a reference on the available format specifiers: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior – Nov 06 '14 at 22:34
-
Ahh I see. Didn't catch that very valuable detail of "!4"vs"2014". Thank you for the answer and many thanks for the reference! – LazerSharks Nov 06 '14 at 22:39
16
If you like to use the dateutil
and its parser:
from dateutil.parser import parse
date1 = parse('10/12/13')
date2 = parse('10/15/13')
print date1 - date2
print date2 > date2

gongzhitaao
- 6,566
- 3
- 36
- 44
14
Here's one solution using datetime.datetime.strptime
:
>>> date1 = datetime.datetime.strptime('10/12/13', '%m/%d/%y')
>>> date2 = datetime.datetime.strptime('10/15/13', '%m/%d/%y')
>>> date1 < date2
True
>>> date1 > date2
False

aIKid
- 26,968
- 4
- 39
- 65
-
for some reason %y (lower case y) gave me "Unconverted data remains " error, while %Y (upper case Y) worked. Why? – LazerSharks Nov 06 '14 at 22:26
-
1`%y` works with year without centuries - as a zero padded number (01, 02, 03.. and so on). While `%Y`works with year with century (decimal) number: (1999, 1979). What's your data? – aIKid Dec 11 '14 at 02:57
4
Use datetime.datetime.strptime
.
from datetime import datetime
a = datetime.strptime('10/12/13', '%m/%d/%y')
b = datetime.strptime('10/15/13', '%m/%d/%y')
print 'a' if a > b else 'b' if b > a else 'tie'

Paul Draper
- 78,542
- 46
- 206
- 285
3
I know this post is 7 years old, but wanted to say that you can compare two date strings without converting them to dates
>>> "10/12/13" > "10/15/13"
False
>>> "10/12/13" < "10/15/13"
True
>>> "10/12/13" == "10/15/13"
False
If there is anything wrong with this approach I would love for someone to tell me.

STIKO
- 1,995
- 1
- 10
- 9
-
4Please refer to this answer for why this is a bad way to compare date strings. https://stackoverflow.com/a/31350422/4589310 – skyfail Mar 18 '21 at 19:40
-
3A counterexample: `'10/15/13' > '10/12/14'` but `October 15, 2013` was before `October 12, 2014`. – Jeyekomon Dec 06 '21 at 13:33
0
import datetime
d1="10/12/13"
d2="10/15/13"
date = d1.split('/')
d1=datetime.datetime(int(date[2]),int(date[1]),int(date[0]))
date = d2.split('/')
d2=datetime.datetime(int(date[2]),int(date[1]),int(date[0]))
if d1 > d2 :
## Code
today = datetime.datetime.today()
if d1 > today :
## code
-
1Please add more details to your answer, maybe a brief explanation of what the code does would help. – Gangula Nov 08 '20 at 11:40
-1
The simplest way to accomplish this is using Pandas
import pandas as pd
d1=pd.to_datetime("10/12/13")
d2=pd.to_datetime("10/12/15")
d1>d2
>>False

Mohsin Asif
- 108
- 4