I want to create a countdown of the number of days until a date. I'm not interested in hours or minutes, just the number of days from the current date until the set date. This is what I have been trying:
NSDate *date = [NSDate date];
NSString *dateString = [date description];
// this **dateString** string will have **"yyyy-MM-dd HH:mm:ss +0530"**
// NSMutableString *mutString = dateString;
NSArray *arr = [dateString componentSeperatedByString:@" "];
// arr will have [0] -> yyyy-MM-dd, [1] -> HH:mm:ss, [2] -> +0530 (time zone)
But it doesn't work because the current date is giving time in minutes and seconds.
//NSDate *currentDate = [[NSDate alloc] init];
NSString *start = [arr objectAtIndex:0];//[NSString stringWithFormat:@"%@", currentDate];//@"2011-11-05";
NSLog(@"%@",start);
NSString *end = @"2011-6-03";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
[f release];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:startDate
toDate:endDate
options:0];
NSInteger days = [components day];
[gregorianCalendar release];
NSLog(@"%i", days);
I don't really understand much of this code, I'm not familar with NSDate and formatter, but this is my combination of a bunch of internet code.
Thanks in advance.