How to calculate the age based on NSDate

后端 未结 11 1917
孤街浪徒
孤街浪徒 2020-12-04 09:07

how to calculate the age based on the birth date in this format 6/24/1976 mon/date/year...

11条回答
  •  [愿得一人]
    2020-12-04 09:33

    the easiest way is to use the NSCalendar:

    NSDate *_dateOfBirth = ...;
    NSDateComponent *_diff = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:_dateOfBirth toDate:[NSDate date] options:0];
    
    NSLog(@"%@", _diff);
    

提交回复
热议问题