1

Thanks Everyone, im close. Im using Python within a survey software tool called Decipher, so it may appear a little different. My issue now is I need to "pipe" in the age based on a respondents answer, that would be your_date_string. But when I put that in my dob variable I get an error from the system: ValueError: time data 'your_date_string' does not match format '%Y,%d,%m', but printing the your_date_string returns the exact value needed. Not sure how to fix this.

from datetime import date

your_date_string = str(2003 - (scr_q2c.c1.val)) +","+ str(scr_q2b.c1.val + 1) +","+ str(scr_q2a.c1.val + 1)
year, day, month = [int(f) for f in your_date_string.split(',')]
your_date = date(year, month, day)

dob = datetime.datetime.strptime('your_date_string', '%Y,%d,%m')
today = datetime.date.today()
age = today.year - dob.year - ((today.month, today.day) < (dob .month, dob .day))

print your_date_string
print age
  • What have you tried so far? What does your actual date format look like? Are the strings all formatted the same? – Engineero Apr 10 '18 at 18:17
  • 1
    dob = str(2003 - (scr_q2c.c1.val)) + "," + str(scr_q2b.c1.val + 1) + "," + str(scr_q2a.c1.val + 1) from datetime import date age = date.today()-date(dob) print(age) print dob – Michael O'Hanlon Apr 10 '18 at 18:17
  • use `datetime`. – Daniel Apr 10 '18 at 18:18
  • 1
    Could you add that to your question? It's hard to read in a comment – c2huc2hu Apr 10 '18 at 18:19
  • What is wrong with your solution? You might make use of [`date.strptime`](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) – wwii Apr 10 '18 at 18:24
  • Im getting an issue where the system is telling me "an integer is required" for dob. But I tried converting to int and still no luck – Michael O'Hanlon Apr 10 '18 at 18:26
  • Possible duplicate of [Python timedelta in years](https://stackoverflow.com/questions/765797/python-timedelta-in-years) – ak_slick Apr 10 '18 at 18:28
  • When asking a question about code that throws an exception, please post the complete Traceback - copy it and paste it into your question then format it as code ... https://stackoverflow.com/help/formatting . Please take the time to read [ask] and the other links found on that page. – wwii Apr 10 '18 at 18:38
  • I just need to convert your_date_string to a useable variable in my dob variable. When I manually input 1982,14,4 it works, just doesnt work when I use the variable name. – Michael O'Hanlon Apr 10 '18 at 19:26

2 Answers2

2

You first need to convert your string to a date object:

from datetime import date

your_date_string = '1982,14,4'
year, day, month = [int(f) for f in your_date_string.split(',')]
your_date = date(year, month, day)

And then use that date object to compute the number of years. A quick and easy way to do that is to rely on dateutil third-party library that you can install using pip install dateutil. Here is an example:

from dateutil.relativedelta import relativedelta

difference_in_years = relativedelta(date.today(), your_date).years

In this example, difference_in_years = 35.

Guybrush
  • 2,680
  • 1
  • 10
  • 17
0

check the month and day, if today less than date of birth than minus one from year difference

import datetime
dob = datetime.datetime.strptime('1982,14,4', '%Y,%d,%m')
today = datetime.date.today()
print today.year - dob.year - ((today.month, today.day) < (dob .month, dob .day))
galaxyan
  • 5,944
  • 2
  • 19
  • 43