How to calculate the age based on NSDate

后端 未结 11 1915
孤街浪徒
孤街浪徒 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:34

    May be I'm wrong, but this is simpler, isn't it?

    NSTimeInterval ageInterval = [birthDate timeIntervalSinceDate:[NSDate date]];
    
    NSInteger age = ABS(ageInterval / (60 * 60 * 24 * 365));
    
    0 讨论(0)
  • 2020-12-04 09:35

    Here's a function to calculate age (Swift 2.1)

    func calculateAge (birthday: NSDate) -> NSInteger {
    
        let calendar : NSCalendar = NSCalendar.currentCalendar()
        let unitFlags : NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day]
        let dateComponentNow : NSDateComponents = calendar.components(unitFlags, fromDate: NSDate())
        let dateComponentBirth : NSDateComponents = calendar.components(unitFlags, fromDate: birthday)
    
        if ( (dateComponentNow.month < dateComponentBirth.month) ||
            ((dateComponentNow.month == dateComponentBirth.month) && (dateComponentNow.day < dateComponentBirth.day))
            )
        {
            return dateComponentNow.year - dateComponentBirth.year - 1
        }
        else {
            return dateComponentNow.year - dateComponentBirth.year
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:42

    A simple to use swift extension to get the age/how old is a NSDate

    extension NSDate {
        var age: Int {
            let calendar: NSCalendar = NSCalendar.currentCalendar()
            let now = calendar.startOfDayForDate(NSDate())
            let birthdate = calendar.startOfDayForDate(self)
            let components = calendar.components(.Year, fromDate: birthdate, toDate: now, options: [])
            return components.year
        }
    }
    

    Exemple:

    NSDate(timeIntervalSince1970:0).age // => 45 (as of nov 2015) Epoch happened 45 year ago !
    

    By the way, you should be careful, this is the western conception of age, the counting is different in some other countries (Korea for exemple).

    0 讨论(0)
  • 2020-12-04 09:42

    Method in Swift 3+ syntax based on cobbal's solution.

    func calculateAgeInYearsFromDateOfBirth (birthday: Date) -> Int {
            let now = Date()
            let calendar = Calendar.current
    
            let ageComponents = calendar.dateComponents([.year], from: birthday, to: now)
            let age = ageComponents.year!
            return age
        }
    
    0 讨论(0)
  • 2020-12-04 09:43

    If u want to find age alone .use below one which include leap year calculation too

    - (NSInteger)age:(NSDate *)dateOfBirth {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
        NSDateComponents *dateComponentsNow = [calendar components:unitFlags fromDate:[NSDate date]];
        NSDateComponents *dateComponentsBirth = [calendar components:unitFlags fromDate:dateOfBirth];
    
        if (([dateComponentsNow month] < [dateComponentsBirth month]) ||
            (([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
            return [dateComponentsNow year] - [dateComponentsBirth year] - 1;
        } else {
            return [dateComponentsNow year] - [dateComponentsBirth year];
        }
    }
    
    0 讨论(0)
提交回复
热议问题