Adobe Flex:从日期计算年龄

时间:2012-05-13 18:06:49

标签: flex date datediff

  

可能重复:
  How to calculate age in Adobe Flex from Date of Birth

用户的DOB在数据库中存储为yyyy-mm-dd,因此我将字段拆分,然后使月份兼容(例如1月= 0月),然后将字段作为日期放入函数中。我确信这个计算DID有效,但现在它没有,我很难为什么。我从来没有真正理解这些代码。

这就是我所拥有的

trace(data.dob);


                // Retrieving the user's DOB
                var dateStr:String = data.dob;
                //Splitting the DOB up to make it compatible with Flex
                var parts:Array = dateStr.split("-");

                if(parts == null || parts.length != 3)
                {
                    Alert.show("There is an error retrieving the birthday.");
                }

                var dobDay:* = parts[2];
                var dobMonth:* = parts[1] - 1;
                var dobYear:* = parts[0];
                // Pasting the DOB back together
                var userDOB : Date = new Date(dobYear, dobMonth, dobDay);
                var today : Date = new Date();
                var diff : Date = new Date();
                //Calculating & printing the Age
                diff.setTime( today.getTime() - userDOB.getTime() );
                var userAge : int = diff.getFullYear() - 1970;
                ageLbl.text = "Age: " + userAge.toString(); 

0 个答案:

没有答案