How can I convert a birthday string into age in actionscript3?
For example if I was given the string "01/01/1980". How could I then get the current date, then figure out how old someone is, and then just return how many years old they are?
I saw a topic on this in C++ but i'm not to familiar with it, anyone know how this would be done in AS开发者_C百科3?
edit: I think what i'm having the hardest time with is how I would break down the original brithday string i'm starting with into month, day, year vars
Man, a lot of people here like convoluted complex code. Here's how you simplify it:
// Parse the date string
var dob:Date = DateFormatter.parseDateString("03/30/2001");
// Get todays timestamp at 00:00:00
var today:Date = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
// Do the age differrence
var age:uint = today.getFullYear() - dob.getFullYear();
// Set DOB to this year
dob.setFullYear(today.getFullYear());
// Check to see if we haven't passed today's date
if(dob.getTime() < today.getTime()){ age--; }
From the tests I've done, this is accurate 100% and I think should be the fastest you can make it since you're not doing any rounding or complex math, just conditionals.
check out this link (GOOGLE IS YOUR FRIEND :-))
The code (direct copy from site):
function calculateAge(birthdate:Date):Number {
var dtNow:Date = new Date();// gets current date
var currentMonth:Number = dtNow.getMonth();
var currentDay:Number = dtNow.getDay();
var currentYear:Number = dtNow.getFullYear();
var bdMonth:Number = birthdate.getMonth();
var bdDay:Number = birthdate.getDay();
var bdYear:Number = birthdate.getFullYear();
// get the difference in years
var years:Number = dtNow.getFullYear() - birthdate.getFullYear();
// subtract another year if we're before the
// birth day in the current year
if (currentMonth < bdMonth || (currentMonth == bdMonth && currentDay < bdDay)) {
years--;
}
return years;
}
function dateStringToObject(dateString):Date {
var date_ar = dateString.split("/");
return new Date(date_ar[2],date_ar[0] - 1,date_ar[1]);
}
var dateNow:Date = new Date();
var checkDate:String = "11/25/1976";
var dateBirthday:Date = dateStringToObject(checkDate);
trace("dateNow = "+dateNow);
trace("dateBirthday = "+dateBirthday);
trace("age = "+calculateAge(dateBirthday));
Check out this question. It has a solution that is really slick.
var today = new Date()
var birthday = new Date(1980, 00, 12) //this is the birthday, you can have this be an input to a function as well if you wanted
var bigToday:int = today.getFullYear()*10000+(today.getMonth()+1)*100+today.getDate();
var bigBDay:int = birthday.getFullYear()*10000+(birthday.getMonth()+1)*100+birthday.getDate();
var diff:int = bigToday-bigBDay
var age:int = diff/10000
trace (age)
var nowDate:Date = new Date( )
var bDate:Date = new Date( '01/01/1980' )
var age:Number = nowDate.getFullYear() - bDate.getFullYear()
if( nowDate.getUTCMonth()*100 + nowDate.getUTCDay() < bDate.getUTCMonth()*100 + bDate.getUTCDay() ){
--age
}
trace(age)
精彩评论