Reputation: 37
dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965',
'18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']
I have split up each element within the list to obtain the year
how do i deduct the birth year from current year? (Eg 2021-1990)
results to be copied into the empty dictionary age[]
Heres my code:
year = 2021
age =[]
dob = []
for x in dob_list:
age +=x[7:11].split("-")
print(age)
Upvotes: 2
Views: 123
Reputation: 1245
If you dont want to use date time, and all you care about is the birth year, then you can simply convert it to an Integer and subtract it from the birth year.
year = 2021
age = [year - int(birthdDate[-4]) for birthDate in dob_list]
Upvotes: 2
Reputation: 24049
You can use datetime
and relativedelta
from the dateutil
package like below:
(you need to install pip install python-dateutil
)
>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta
>>> dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965', '18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']
>>> [relativedelta(datetime.now(),datetime.strptime(dob, '%d-%b-%Y')).years for dob in dob_list]
[31, 26, 39, 33, 44, 55, 43, 27, 40, 40]
# for more explanation
>>> [relativedelta(datetime.now(),datetime.strptime(dob, '%d-%b-%Y')) for dob in dob_list]
[relativedelta(years=+31, months=+9, days=+13, hours=+16, minutes=+50, seconds=+14, microseconds=+585158),
relativedelta(years=+26, months=+2, days=+3, hours=+16, minutes=+50, seconds=+14, microseconds=+585268),
relativedelta(years=+39, months=+5, days=+29, hours=+16, minutes=+50, seconds=+14, microseconds=+585325),
relativedelta(years=+33, months=+6, days=+24, hours=+16, minutes=+50, seconds=+14, microseconds=+585379),
relativedelta(years=+44, months=+10, days=+19, hours=+16, minutes=+50, seconds=+14, microseconds=+585426),
relativedelta(years=+55, months=+10, days=+7, hours=+16, minutes=+50, seconds=+14, microseconds=+585469),
relativedelta(years=+43, months=+9, days=+26, hours=+16, minutes=+50, seconds=+14, microseconds=+585504),
relativedelta(years=+27, months=+4, days=+19, hours=+16, minutes=+50, seconds=+14, microseconds=+585546),
relativedelta(years=+40, days=+5, hours=+16, minutes=+50, seconds=+14, microseconds=+585587),
relativedelta(years=+40, months=+7, days=+25, hours=+16, minutes=+50, seconds=+14, microseconds=+585620)]
Upvotes: 3
Reputation: 768
I suggest using datetime
module in order to be able to compute the exact date and avoid splitting issues of your date strings.
The function calculate_age
allows you to compute the exact age of a person. Credits to this answer.
Below is the full code.
import datetime
dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965', '18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']
date_now = datetime.datetime.now()
age =[]
def calculate_age(date_now, born_date):
return date_now.year - born_date.year - ((date_now.month, date_now.day) < (born_date.month, born_date.day))
for x in dob_list:
date = datetime.datetime.strptime(x, '%d-%b-%Y')
age.append(calculate_age(date_now, date))
print(age)
# Output: [31, 26, 39, 33, 44, 55, 43, 27, 40, 40]
Upvotes: 4
Reputation: 4092
You can simply get the last 4 characters of each string like this:
for i in dates:
date = i[-4:]
But you must remember that year1-year2
doesn't return an age.
Otherwise, you can use the datetime
module in the scope of getting the date, in particular a datetime.datetime.date
object.
The input format should be something like dd-mm-yyyy
.
from datetime import datetime
You can find a tutorial about this module here, and the documentation there.
Then you can look at this answer to better understand how to get how many year passed between two dates (in that case your date and datetime.datetime.now()
).
Upvotes: 2