To calculate the age from a birthdate in Python, use this function:,The correct way to calculate age in Python is using datetime.date objects: ,Today you learned how to calculate age with Python’s datetime.date objects.,As you saw, the code subtracts one from the year if today’s month/day precedes the birthday’s month/day.
To calculate the age from a birthdate in Python, use this function:
from datetime
import date
def age(birthdate):
today = date.today()
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
return age
print(age(date(2000, 1, 1)))
Output:
21
It does this by comparing tuples of integers:
(today.month, today.day) < (birthday.month, birthday.day)
Let’s run a simple example:
>>> (2, 5) < (2, 9) True >>> (3, 1) < (2, 29) False >>>
You may use
( ? : A | A | AA F | M)\ d + ( ? : -\d + | \+)
import re s = "" "A25-54 plus affinities targeting,Demo (AA F21-54), A25 - 49 Artist Affinity Targeting, M21 - 49 plus, plus plus A 21 + targeting "" " print(re.findall(r '(?:A|A |AA F|M)\d+(?:-\d+|\+)', s)) # => ['A25-54', 'AA F21-54', 'A25-49', 'M21-49', 'A 21+']
Last Updated : 27 Jul, 2021,GATE CS 2021 Syllabus
Input: 1997 / 2 / 3
Output: 21 years(
for present year i.e 2018)
Input: 2010 / 12 / 25
Output: 8 years(
for present year i.e 2018)
21 years
21 years
Python provides an inbuilt datetime module. It has several classes which provide a number of functions to deal with dates, times, and time intervals. To calculate the age, we simply subtract the birth year from the current year. To implement this, we need to check if the current month and date are less than the birth month and date. If it is, subtract 1 from the age, otherwise 0.,In this process, we find the numeric value of the date from the birth date to the current date. To convert the number of days in the year, simply divide the number of dates by the number of days in a year.,In this post, you will learn how to calculate the present age in years using the Python programming language. Such a type of question is generally asked in a programming interview. Calculating age is a very simple process. We will have to find the difference between the current year and the birth year. Python provides various approaches to calculate the age. Here, we have mentioned most of them.,In this article, we have mentioned the analyzed results of the best programming language for 2021...
Python provides an inbuilt datetime module. It has several classes which provide a number of functions to deal with dates, times, and time intervals. To calculate the age, we simply subtract the birth year from the current year. To implement this, we need to check if the current month and date are less than the birth month and date. If it is, subtract 1 from the age, otherwise 0.
# Python3 program to calculate age in years
from datetime
import date
def calculateAge(dob):
today = date.today()
age = today.year - dob.year - ((today.month, today.day) <
(dob.month, dob.day))
return age
print(calculateAge(date(2017, 9, 18)), " years old")
3 years old
In this process, we find the numeric value of the date from the birth date to the current date. To convert the number of days in the year, simply divide the number of dates by the number of days in a year.
# Python program to calculate age in years
from datetime
import date
def calculate_age(dob):
days_in_year = 365.2425
age = int((date.today() - dob).days / days_in_year)
return age
print(calculate_age(date(1989, 7, 1)), " years old")
Edited ( June 17, 2020 ) Edit
Lets consider the following date of birth: July 8, 1982:
import datetime
dob = datetime.date(1982, 8, 7)
to get the age of that person at the present day (June 16, 2020), a solution is to defined a function:
def from_dob_to_age(born):
today = datetime.date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
that returns here
from_dob_to_age(dob)
in thta case it is necessary to first convert the string to datetime (see Python string to datetime – strptime() pour plus de details):
dob = datetime.datetime.strptime(dob, '%Y-%m-%d')
and then call the function defined above:
from_dob_to_age(dob)
The AGE() function accepts two TIMESTAMP values. It subtracts the second argument from the first one and returns an interval as a result.,If you want to take the current date as the first argument, you can use the following form of the AGE() function:,In this example, use the AGE() function to calculate the rental duration based on the values of the rental_date and return_date columns. The following shows the output:,The following illustrates the syntax of the AGE() function:
The following illustrates the syntax of the AGE()
function:
.wp - block - code {
border: 0;
padding: 0;
}
.wp - block - code > div {
overflow: auto;
}
.shcb - language {
border: 0;
clip: rect(1 px, 1 px, 1 px, 1 px); -
webkit - clip - path: inset(50 % );
clip - path: inset(50 % );
height: 1 px;
margin: -1 px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1 px;
word - wrap: normal;
word - break: normal;
}
.hljs {
box - sizing: border - box;
}
.hljs.shcb - code - table {
display: table;
width: 100 % ;
}
.hljs.shcb - code - table > .shcb - loc {
color: inherit;
display: table - row;
width: 100 % ;
}
.hljs.shcb - code - table.shcb - loc > span {
display: table - cell;
}
.wp - block - code code.hljs: not(.shcb - wrap - lines) {
white - space: pre;
}
.wp - block - code code.hljs.shcb - wrap - lines {
white - space: pre - wrap;
}
.hljs.shcb - line - numbers {
border - spacing: 0;
counter - reset: line;
}
.hljs.shcb - line - numbers > .shcb - loc {
counter - increment: line;
}
.hljs.shcb - line - numbers.shcb - loc > span {
padding - left: 0.75 em;
}
.hljs.shcb - line - numbers.shcb - loc::before {
border - right: 1 px solid #ddd;
content: counter(line);
display: table - cell;
padding: 0 0.75 em;
text - align: right; -
webkit - user - select: none; -
moz - user - select: none; -
ms - user - select: none;
user - select: none;
white - space: nowrap;
width: 1 % ;
}
AGE(timestamp, timestamp);
Code language: SQL(Structured Query Language)(sql)
See the following example:
SELECT AGE('2017-01-01', '2011-06-24');
Code language: SQL(Structured Query Language)(sql)
Code language: SQL (Structured Query Language) (sql)SELECT AGE('2017-01-01','2011-06-24');
AGE
-- -- -- -- -- -- -- -- -- -- -- -
5 years 6 mons 7 days
(1 row)
Code language: Shell Session(shell)
For example, if someone has a birth date2000-01-01
and the current date is 2017-03-20
, his/her age will be:
SELECT current_date,
AGE(timestamp '2000-01-01');
Code language: SQL(Structured Query Language)(sql)
Code language: SQL (Structured Query Language) (sql)SELECT current_date, AGE(timestamp '2000-01-01');
date | AGE -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- -- -- - 2017 - 03 - 20 | 17 years 2 mons 19 days(1 row) Code language: Shell Session(shell)
In this example, we are calculating the age by converting dates difference in milliseconds. It is also an easy way to calculate age.,There are various ways of calculating age. It is one more example to calculate age in year, month, and days format.,There are various ways to calculate the age from date of birth. We will discuss simple and easily understandable methods to calculate the age using JavaScript.,By executing the above code, an HTML form will appear. Here, choose a date (date of birth) from the calendar and click on the Calculate Age button to calculate the age from the provided date of birth.
Age of the date entered: 12 years
JavaScript seems to be disabled in your browser. You must have JavaScript enabled in your browser to utilize the functionality of this website.
from datetime
import date
def calculate_age(born):
today = date.today()
try:
birthday = born.replace(year = today.year)
except ValueError:
birthday = born.replace(year = today.year, month = born.month + 1, day = 1)
if birthday > today:
return today.year - born.year - 1
else:
return today.year - born.year
print(calculate_age(date(2001, 3, 1)))
20