Calculate age from BirthDate

后端 未结 17 1343
攒了一身酷
攒了一身酷 2021-02-07 08:33

I have DatePicker Dialog, When I select date at that time I want to calculate age it\'s working but when I select date of current year at that time it showing the -1 age instead

17条回答
  •  走了就别回头了
    2021-02-07 08:53

    New Language Dart using DateTime

    static int getPerfectAgeInYears(DateTime dob,DateTime today) {
    
    dob = DateTime(dob.year,dob.month,dob.day);
    
    int ageInteger = 0;
    
    today = DateTime(today.year,today.month,today.day);
    
    ageInteger = today.year-dob.year;
    
    if (today.month == dob.month) {
    
      if (today.day < dob.day) {
    
        ageInteger = ageInteger - 1;
      }
    
    } else if (today.month < dob.month) {
    
      ageInteger = ageInteger - 1;
    
    }
    
    return ageInteger;}
    

    Call as print(getPerfectAgeInYears(DateTime(2000,6,4),DateTime.now())); Consider Today's Date - 30th August 2020

    If Birthdate - 29th July 1993, the output - 27

    If Birthdate - 29th August 1993, the output - 27

    If Birthdate - 30th August 1993, the output - 27

    If Birthdate - 31st August 1993, the output - 26

    If Birthdate - 31st September 1993, the output - 26

提交回复
热议问题