0

I have a script that gets a string in the form of 3/4/2013. How can I convert that to a date that I can then use to determine the age of the date (in months)? I would also like to be able to have the month in decimal form (i.e. 2.8 months old).

I'm not sure what to do at all as far as coding this. I've read about different libraries that can do things like this, but I'm not sure what one is best.

EDIT: Assume a month has 30 days. This is what I have so far:

import time
def ageinmonths( str )
    return time.today() - time.strptime("3/4/2013", "%b %d %y")
xiº
  • 4,605
  • 3
  • 28
  • 39
JStew
  • 437
  • 3
  • 7
  • 19
  • 3
    What have you tried so far? The `datetime` module is a good place to start. – g.d.d.c Jun 14 '13 at 16:36
  • http://docs.python.org/2/library/datetime.html – Nick T Jun 14 '13 at 16:37
  • How do you define a month? 30 days or by calendar? If someone is born on Feb 28 in a leap year are they 0.0667 (1/29 + 1/31) months old two days later? – dansalmo Jun 14 '13 at 16:46
  • This is going to depend largely on how you calculate "1 month". Is 1 month = 30 days? Is it when you flip the page on the calendar? – Andenthal Jun 14 '13 at 16:46
  • I suggest that you use idle or another Python shell and enter expressions like `time.today()` and `time.strptime("3/4/2013", "%b %d %y")` and play with them to see what happens. Then `import datetime` and do `dir(datetime)` – dansalmo Jun 14 '13 at 16:53
  • I have edited my submission with what I've written so far. – JStew Jun 14 '13 at 16:53

5 Answers5

3

This is the answer from kzh but with the addition of the decimal that the poster wanted.

from datetime import datetime
from dateutil.relativedelta import relativedelta

date = '3/4/2013'

dt = datetime.strptime(date,'%m/%d/%Y')
r = relativedelta(datetime.now(), dt)
months = r.years * 12 + r.months + r.days/30.
print months
>>>> 3.33333333333
Chris Hagmann
  • 1,086
  • 8
  • 14
  • 1
    Upvote to you for taking my answer and making it the actual answer. Most of the other answers are not pythonic. One of the most valuable comments regarding Python that I have seen came from one of my old questions: "It's not at all pythonic to rewrite a function that the standard library provide". http://stackoverflow.com/questions/1233539/python-dictionary-to-url-parameters#comment1059527_1233857 – kzh Jun 14 '13 at 18:23
2

Something like this:

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> dt = datetime.strptime('3/4/2013','%m/%d/%Y')
>>> r = relativedelta(datetime.now(), dt)
>>> months = r.years * 12 + r.months + r.days/30
kzh
  • 19,810
  • 13
  • 73
  • 97
2

EDIT: for ultimate transatlantic date-formatting harmony!

import datetime,locale

def transatlantic_age_in_months(datestring):
    datefmt = locale.nl_langinfo(locale.D_FMT)
    dob = datetime.datetime.strptime(datestring,datefmt[:-2]+'%Y')
    dt = dob.today() - dob
    return dt.days/30.
ali_m
  • 71,714
  • 23
  • 223
  • 298
0

I would use str.split('/') and then manipulate each index of the list that way.
This way means you can work with the day/month/year individually to calculate the necessary values.

DragonVet
  • 399
  • 2
  • 6
  • 19
0

High-level approach: Convert your date to a datetime object. Call datetime.now(). Subtract. Noodle result into months.

JS.
  • 14,781
  • 13
  • 63
  • 75