NSDate+ComponentsExtractor.h:
typedef struct
{
NSInteger year;
NSInteger month;
NSInteger day;
} dateComponents;
@interface NSDate (ComponentsExtractor)
+ (dateComponents)componentsFromDate:(NSDate *)theDate;
@end
NSDate+ComponentsExtractor.m:
#import "NSDate+ComponentsExtractor.h"
@implementation NSDate (ComponentsExtractor)
+ (dateComponents)componentsFromDate:(NSDate *)theDate
{
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:
NSGregorianCalendar] autorelease];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:theDate];
dateComponents theComponents;
theComponents.year = [components year];
theComponents.month = [components month];
theComponents.day = [components day];
return theComponents;
}
@end
Making use of the category:
NSDate *theDate = [NSDate date]; // use the current date ...
dateComponents components = [NSDate componentsFromDate:theDate];
NSLog(@"%i - %i - %i", components.year, components.month, components.day);
No comments:
Post a Comment