0

I would like to display the number of days between now and a date I have stored in a NSDate. Is there a way to get the amount of days between two NSDate objects?????

Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • 1
    check my answer in this http://stackoverflow.com/questions/25202497/countdown-timer-between-two-dates/25202565#25202565 – Mohit Aug 29 '14 at 13:57

2 Answers2

0

I have published an open-source class/library to do just this.

Have a look at RelativeDateDescriptor, which can be used to obtain the time difference as follows...

RelativeDateDescriptor *descriptor = [[RelativeDateDescriptor alloc] initWithPriorDateDescriptionFormat:@"%@ ago" postDateDescriptionFormat:@"in %@"];

// date1: 1st January 2000, 00:00:00
// date2: 6th January 2000, 00:00:00
[descriptor describeDate:date2 relativeTo:date1]; // Returns '5 days ago'
[descriptor describeDate:date1 relativeTo:date2]; // Returns 'in 5 days'
mmccomb
  • 13,516
  • 5
  • 39
  • 46
-1

Try this it is working for me.

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-ddHH:mm:ss"]; // your date format
NSDate *startDate = [NSDate date]; // Today's date
NSString * end = @"2014-08-2312:00:00"; // your date
NSDate *endDate = [f dateFromString:end];

NSUInteger unitFlags = NSDayCalendarUnit;
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate
                                                 options:0];

NSLog(@"%ld Days Left",(long)components.day);
Mohit
  • 3,708
  • 2
  • 27
  • 30