Skip to content

Instantly share code, notes, and snippets.

@g761007
Created July 6, 2018 12:16
Show Gist options
  • Save g761007/74e4fe342dd1a21f0515add2f4120c26 to your computer and use it in GitHub Desktop.
Save g761007/74e4fe342dd1a21f0515add2f4120c26 to your computer and use it in GitHub Desktop.
The NSDate extension for calculating the age.
//
// NSDate+Age.h
//
// Created by Daniel Hsieh on 2018/7/6.
//
#import <Foundation/Foundation.h>
@interface NSDate (Age)
- (NSInteger)age;
@end
//
// NSDate+Age.m
//
// Created by Daniel Hsieh on 2018/7/6.
//
#import "NSDate+Age.h"
@implementation NSDate (Age)
- (NSInteger)age {
return [[NSCalendar currentCalendar] components:NSCalendarUnitYear
fromDate:self
toDate:[NSDate date]
options:0].year;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment