Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please help with "date of birth" to "age" conversion

Status
Not open for further replies.

LordMerlin

IS-IT--Management
Jan 19, 2006
1
ZA
Hi everyone.

I'm trying to incorporate a date of birth to age calculator on my site. Currently I have the following <a href= target=_new>Date Validation Script</a> which validates the date of birth to make sure it's in the dd/mm/yyyy format.

Then, I have the following piece of code, which calculates the age, according to the date of birth

Code:
<script language="javascript" type="text/javascript">
<!--
function calc(form) {
	var today= new Date();
	var birthday= new Date(document.form1.dob.value);
	secold=(today.getYear()-birthday.getYear())*31557600+(today.getMonth()-birthday.getMonth())*2629800+(today.getDay()-birthday.getDay())*86400+today.getHours()*3600+today.getMinutes()*60+today.getSeconds()

	document.form1.age.value=Math.floor(((secold/3600)/24)/365.25);
}
//-->
</script>

My problem is, this age calculator expects the date format to be mm/dd/yyyy. Is there a way of specifying that my date in the the format of dd/mm/yyyy?
 
This script will return the age if you pass it a date in the dd/mm/yyyy format:
Code:
function getAge(_data) {
	var _day = _data.split('/')[0];
	var _month = _data.split('/')[1];
	var _year = _data.split('/')[2];
	var _milliseconds_in_a_day = 1000*60*60*24;
	var birth = new Date(_year, _month-1, _day);
	var today = new Date();
	var days_since_birth = Math.ceil((today - birth)/_milliseconds_in_a_day) - 1;
	var age = Math.floor(days_since_birth/365);
	return age;
}
I have left it expanded... here is a condensed version of the same function:
Code:
function getAgeCondensed(_data) {
  return Math.floor((Math.ceil(((new Date()) - (new Date(_data.split('/')[2], _data.split('/')[1]-1, _data.split('/')[0])))/(1000*60*60*24)) - 1)/365);
}
You can test it using this:
Code:
<a href="javascript:alert(getAge('27/01/1756'))">How old is Mozart?</a>

Hope that helps!

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
I'm sure Jeff's script is fine.

A way to state this is that you need to do a little string manipulation in place of:

Code:
var birthday= new Date(document.form1.dob.value);

Something like:
Code:
var formVal = document.form1.dob.value;
var _data = formVal;
//use Jeff's string manipulation here
var birthday = new Date(_year, _month-1, _day);

It's probably a bit overkill on your part to consider the hour of the day, etc. as you are probably not asking the user for the hour of their birth and one generally accepts that midnight on the anniversary of their birth represents the start of their "birthday".

Wouldn't that be fun, though, if instead of celebrating people's birthdays, we celebrated their birthhours? ...or birthminutes? And if the unit of birthdays in years, then the unit for birthhours would be days, I guess. ...and birthminutes, hours.

I need to take a walk.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Try this:

=DateDiff("yyyy", [Birthdate], Now())+ Int( Format(now(), "mmdd") < Format( [Birthdate], "mmdd") )

It works just fine!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top