0

I'm trying to find the difference between these two dates or any other dates for that matter

a = '11-Feb-2003'
b = '28-Aug-2015'

I'm looking for a way (something not so manual/diy) to convert the months (Feb & August) into integers?

Should I create a dictionary?

To be even more specific I'd like to turn the dates into lists.

'11-Feb-2003' becomes [11, 2, 2003] = startdate

'28-Aug-2015' becomes [28, 8, 2015] = enddate

From there I can calculate the difference in number of days between the two by executing:

import datetime
datetime.date(enddate[2], enddate[1], enddate[0]) - 
dateimdate(startdate[2], startdate[1], startdate[0])
  • Possible duplicate of [Converting string into datetime](http://stackoverflow.com/questions/466345/converting-string-into-datetime) – rmunn Jan 14 '16 at 04:52

6 Answers6

1

You can use Python's datetime module to convert a string to a date object.

The strptime function takes a string and the format of the date.

from datetime import datetime
print datetime.strptime('11-Feb-2003', '%d-%b-%Y')

%d is for Day of the month as a zero-padded decimal number. (01, 02, ..., 31)
%b is for Month as locale’s abbreviated name. (Jan, Feb, ..., Dec)
%Y is for Year with century as a decimal number. (1970, 1988, 2001, 2013)

Here is a list of all the format specifiers for your reference.

Resley Rodrigues
  • 2,218
  • 17
  • 17
1

To find the difference between two strings that represent dates, you first need to convert them to Python's date type, then simply subtract them:

>>> import datetime
>>> s1 = '11-Feb-2003'
>>> s2 = '28-Aug-2015'
>>> d1 = datetime.datetime.strptime(s1, '%d-%b-%Y')
>>> d2 = datetime.datetime.strptime(s2, '%d-%b-%Y')

Now, once your subtract the two datetime objects, you'll get a special datetime.timedelta object:

>>> i = d2-d1
>>> i
datetime.timedelta(4581)

You can get a friendly representation of the difference if you print the object (or convert it to a string):

>>> print(i)
4581 days, 0:00:00

You can also query the object, for example:

>>> i.days
4581
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

You can use strptime.

import datetime as d
d.datetime.strptime(a, '%d-%b-%Y')
#=> datetime.datetime(2003, 2, 11, 0, 0)
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
0

you can use time module:

>>> import time
>>> a = '11-Feb-2003'
>>> b = '28-Aug-2015'
>>> time.strptime(a, "%d-%b-%Y") # %b Locale’s abbreviated month name.
time.struct_time(tm_year=2003, tm_mon=2, tm_mday=11, tm_hour=0,tm_min=0, tm_sec=0, tm_wday=1, tm_yday=42, tm_isdst=-1)
>>> my_date = time.strptime(a, "%d-%b-%Y")
>>> "{}-{}-{}".format(my_date.tm_mday, my_date.tm_mon, my_date.tm_year)
'11-2-2003'
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
0

You can use the split function:

a.split("-")

and that will return:

["11", "Feb", "2003"]
Vamoos
  • 327
  • 4
  • 10
0
import datetime
datetime.datetime.strptime(month, '%B').month

Use %B for full name of the month (e.g. January), %b for short version (e.g. Jan)

Roxana Tapia
  • 115
  • 9