-1

I am having two dates like this,

NSString *givendate = @"2016-10-07T13:01:20";
NSDate *currentDateTime = [NSDate date];

I want to calculate no of days like 5days or 6 days by comparing current date with given date.

I tried the following code.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFromString = [dateFormatter dateFromString:givendate];
NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
NSString *intervalString = [NSString stringWithFormat:@"%f", diff];
cell.days.text=intervalString;

But it prints "nan". any help will be appreciated.

3 Answers3

1

You have to specify dateFormat for NSDateFormatter according to your date string like below, otherwise it will return nil or nan:

    NSDate *dateFromString;

    NSString *givendate = @"2016-10-07T13:01:20";
    NSDate *currentDateTime = [NSDate date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];//Set format here accordingly
    dateFromString = [dateFormatter dateFromString:givendate];
    NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
    NSString *intervalString = [NSString stringWithFormat:@"%f", diff];

Above code is working for me.

Edit:

int num_days = diff / (60 * 60 * 24);
int num_seconds -= num_days * (60 * 60 * 24);
int num_hours = num_seconds / (60 * 60);
int num_seconds -= num_hours * (60 * 60);
int num_minutes = num_seconds / 60;

OR

Use NSGregorianCalendar like below:

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:dateFromString
                                              toDate:currentDateTime options:0];
    NSInteger years = [components year];
    NSInteger months = [components month];
    NSInteger days = [components day]; 
    NSLog(@"years = %ld months = %ld days = %ld",years,months,days);
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

I'm not sure but maybe its because you didn't set the date format :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; //According to date and time format;
dateFromString = [dateFormatter dateFromString:givendate];
NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
NSString *intervalString = [NSString stringWithFormat:@"%f", diff];
cell.days.text=intervalString;
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
0

For "It prints 1050035.00 like this . But I need like 5 days or 6 days like that. Any help ?"

As it returns in milliseconds, so you need to convert it into days.

NSCalendar *_calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit _units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *_components = [_calendar components:_units fromDate:[NSDate date] toDate:[NSDate dateWithTimeIntervalSinceNow:_timeInSeconds] options:kNilOptions];

NSLog(@"%ld Days, %ld Hours, %ld Minutes, %ld Seconds", _components.day, _components.hour, _components.minute, _components.second);

In your case, Output : 12 Days, 3 Hours, 40 Minutes, 35 Seconds

Nikunj Rajyaguru
  • 196
  • 1
  • 11