我正在尝试计算flex的年龄。我发现了上一个问题What is the best way to calculate Age using Flex?
我有点倾向于这个
private function calculateAge(dob:Date):String {
var now:Date = new Date();
var ageDays:int = 0;
var ageYears:int = 0;
var ageRmdr:int = 0;
var diff:Number = now.getTime()-dob.getTime();
ageDays = diff / 86400000;
ageYears = Math.floor(ageDays / 365.24);
ageRmdr = Math.floor( (ageDays - (ageYears*365.24)) / 30.4375 );
if ( ageRmdr == 12 ) {
ageRmdr = 11;
}
return ageYears + " years " + ageRmdr + " months"; }
但我不明白100%的事情正在发生。
如何将此实现到我的代码中,如果日期是12/23/1990?
另外,如果提供两个日期而不是使用当前日期,我将如何修改此代码以计算年龄?例如。 12/23/1990 - 10/15/1999
谢谢!
答案 0 :(得分:0)
我想可以改变如下:
private function calculateAge(dob:Date, startfrom:Date):String {
var ageDays:int = 0;
var ageYears:int = 0;
var ageRmdr:int = 0;
var diff:Number = startfrom.getTime()-dob.getTime();
ageDays = diff / 86400000;
ageYears = Math.floor(ageDays / 365.24);
ageRmdr = Math.floor( (ageDays - (ageYears*365.24)) / 30.4375 );
if ( ageRmdr == 12 ) {
ageRmdr = 11;
}
return ageYears + " years " + ageRmdr + " months";
}
答案 1 :(得分:0)
Date.getTime()返回自1970年1月1日以来的毫秒数,因此diff是两个日期之间的毫秒数。代码除以86400000,因为这是每天的毫秒数。其余的应该是显而易见的。
要修改功能以允许输入两个日期,您可以更改此功能:
private function calculateAge(dob:Date, endDate:Date):String {
var ageDays:int = 0;
var ageYears:int = 0;
var ageRmdr:int = 0;
var diff:Number = endDate.getTime() - dob.getTime();
ageDays = diff / 86400000;
ageYears = Math.floor(ageDays / 365.24);
ageRmdr = Math.floor( (ageDays - (ageYears*365.24)) / 30.4375 );
if ( ageRmdr == 12 ) {
ageRmdr = 11;
}
return ageYears + " years " + ageRmdr + " months";
}