Here is another solution - this is a good exercise because it requires you to "get your hands dirty" with the NSDate documentation. I also tip my hat to the solutions above. Nice job!
ElapsedDays.h//
// ElapsedDays.h
// DateStuff
//
// Created by Dennis Pieretti on 9/1/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
// Add new NSDate category ElapsedDays
@interface NSDate (ElapsedDays)
-(unsigned long) numberOfElapsedDays: (NSDate *) theDate;
@end
ElapsedDays.m//
// ElapsedDays.m
// DateStuff
//
// Created by Dennis Pieretti on 9/1/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ElapsedDays.h"
@implementation NSDate (ElapsedDays)
-(unsigned long) numberOfElapsedDays: (NSDate *) theDate
{
unsigned long theTimeInSecondsElapsed = [self timeIntervalSinceDate:theDate];
return theTimeInSecondsElapsed / (24 * 60 * 60);
}
@end
main.m//
// main.m
// DateStuff
//
// Created by Dennis Pieretti on 9/1/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
// Solution to Ch15 #01
#import <Foundation/Foundation.h>
#import "ElapsedDays.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// set dates to find days between dates "yyyy-mm-dd hh:mm:ss +GMToffset"
NSDate *dateOne = [NSDate dateWithString:@"2011-09-09 12:00:00 +0000"];
NSDate *dateTwo = [NSDate dateWithString:@"2011-09-11 12:00:00 +0000"];
// select a format style for date output from: ShortStyle, MediumStyle, LongStyle, FullStyle
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:(NSDateFormatterMediumStyle)];
// display the computed days between dates
NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
[dateFormat stringFromDate: dateOne],
[dateFormat stringFromDate: dateTwo],
[dateTwo numberOfElapsedDays: dateOne]);
dateOne = [NSDate dateWithString:@"2011-02-28 12:00:00 +0000"];
dateTwo = [NSDate dateWithString:@"2011-03-01 12:00:00 +0000"];
// display the computed days between dates
NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
[dateFormat stringFromDate: dateOne],
[dateFormat stringFromDate: dateTwo],
[dateTwo numberOfElapsedDays: dateOne]);
// test leap year
dateOne = [NSDate dateWithString:@"2012-02-28 12:00:00 +0000"];
dateTwo = [NSDate dateWithString:@"2012-03-01 12:00:00 +0000"];
// display the computed days between dates
NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
[dateFormat stringFromDate: dateOne],
[dateFormat stringFromDate: dateTwo],
[dateTwo numberOfElapsedDays: dateOne]);
// new year
dateOne = [NSDate dateWithString:@"2012-12-31 12:00:00 +0000"];
dateTwo = [NSDate dateWithString:@"2013-01-01 12:00:00 +0000"];
// display the computed days between dates
NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
[dateFormat stringFromDate: dateOne],
[dateFormat stringFromDate: dateTwo],
[dateTwo numberOfElapsedDays: dateOne]);
// one year
dateOne = [NSDate dateWithString:@"2012-01-01 12:00:00 +0000"];
dateTwo = [NSDate dateWithString:@"2012-12-31 12:00:00 +0000"];
// display the computed days between dates
NSLog(@"\nThe number of days elapsed from %@ to %@ is %lu",
[dateFormat stringFromDate: dateOne],
[dateFormat stringFromDate: dateTwo],
[dateTwo numberOfElapsedDays: dateOne]);
[pool drain];
return 0;
}
SAMPLE OUTPUT2011-09-02 19:57:25.842 DateStuff[280:903]
The number of days elapsed from Sep 9, 2011 to Sep 11, 2011 is 2
2011-09-02 19:57:25.844 DateStuff[280:903]
The number of days elapsed from Feb 28, 2011 to Mar 1, 2011 is 1
2011-09-02 19:57:25.844 DateStuff[280:903]
The number of days elapsed from Feb 28, 2012 to Mar 1, 2012 is 2
2011-09-02 19:57:25.846 DateStuff[280:903]
The number of days elapsed from Dec 31, 2012 to Jan 1, 2013 is 1
2011-09-02 19:57:25.846 DateStuff[280:903]
The number of days elapsed from Jan 1, 2012 to Dec 31, 2012 is 365
Program ended with exit code: 0