Skip to content

Instantly share code, notes, and snippets.

@alex-cellcity
Created August 7, 2012 07:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alex-cellcity/3282790 to your computer and use it in GitHub Desktop.
Save alex-cellcity/3282790 to your computer and use it in GitHub Desktop.
Calculate age from NSDate
//
// NSDate+Age.h
//
// Created by Alex Yao on 7/8/12.
// Copyright (c) 2012 alex@thecellcity.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDate (Age)
- (NSInteger)age;
@end
//
// NSDate+Age.m
//
// Created by Alex Yao on 7/8/12.
// Copyright (c) 2012 alex@thecellcity.com. All rights reserved.
//
#import "NSDate+Age.h"
@implementation NSDate (Age)
- (NSInteger)age
{
NSDateComponents *timeOnEarth = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:self toDate:[NSDate date] options:0];
return [timeOnEarth year];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment