Calculating Age Derived from Current Date and Dob

Calculating Age Using DoB from a Specific Date in the past

Your calculations are wrong. That person should be 28 at that time.

A simple trick is to treat the date as a number in the form of yyyyMMdd and then subtract the target date from DOB and get the integer part. With your sample dates:

DECLARE @dob DATETIME='19910218';
DECLARE @targets TABLE(target DATETIME);
INSERT @targets(target)VALUES('20200201'), (GETDATE());
SELECT
FLOOR(((YEAR(target)* 10000+MONTH(target)* 100+DAY(target))
-(YEAR(@dob)* 10000+MONTH(@dob)* 100+DAY(@dob))
)/ 10000
)
FROM @targets;

PS: Floor might be unnecessary, SQL server makes an integer division but I would like to keep it as a safety.

Oracle Age calculation from Date of birth and Today

SQL> select trunc(months_between(sysdate,dob)/12) year,
2 trunc(mod(months_between(sysdate,dob),12)) month,
3 trunc(sysdate-add_months(dob,trunc(months_between(sysdate,dob)/12)*12+trunc(mod(months_between(sysdate,dob),12)))) day
4 from (Select to_date('15122000','DDMMYYYY') dob from dual);

YEAR MONTH DAY
---------- ---------- ----------
9 5 26

SQL>

How to get an age from a D.O.B field in MySQL?

A few ways:

select DATEDIFF(customer.dob, '2010-01-01') / 365.25 as age

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(customer.dob,'2010-01-01')), ‘%Y')+0 AS age

Hope this helps you

Calculate age from BirthDate

Here is a Java method called getAge which takes integers for year month and day and returns a String type which holds an integer that represents age in years.

private String getAge(int year, int month, int day){
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();

dob.set(year, month, day);

int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
age--;
}

Integer ageInt = new Integer(age);
String ageS = ageInt.toString();

return ageS;
}

Calculate age based on date of birth

PHP >= 5.3.0

# object oriented
$from = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $from->diff($to)->y;

# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;

demo

functions: date_create(), date_diff()



MySQL >= 5.0.0

SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age

demo

functions: TIMESTAMPDIFF(), CURDATE()



Related Topics



Leave a reply



Submit