1

I am trying to subtract two different time values

**Aim:-**i want to subtract the value get the difference between two time which I get from server

below is my method

        self.start=[jsonDict valueForKey:@"start_time"];
        self.end=[jsonDict valueForKey:@"end_time"];
        self.datefj=[jsonDict valueForKey:@"date"];
        NSLog(@"%@",self.start);
        NSLog(@"%@",self.end);
        NSLog(@"%@",self.datefj);
        self.date1fj=[NSString stringWithFormat:@"%@ %@",self.datefj,self.start];
        self.date2fj=[NSString stringWithFormat:@"%@ %@",self.datefj,self.end];
        //NSTimeInterval secondsBetween = [_end timeIntervalSinceDate:_start];
        //NSArray *hoursMins = [_end componentsSeparatedByString:@":"];
        //NSInteger timeInMins = [hoursMins[0] intValue] * 60 + [hoursMins[1] intValue];
        NSDateFormatter *df=[[NSDateFormatter alloc] init];
        // Set the date format according to your needs
        [df setDateFormat:@"dd/MM/YYYY hh:mm"]; //for 12 hour format
        //[df setDateFormat:@"MM/dd/YYYY HH:mm "]  // for 24 hour format

        NSDate *date1 = [df dateFromString:_date1fj];
        NSDate *date2 = [df dateFromString:_date2fj];
        NSLog(@"%@",date1);
        NSLog(@"%@",date2);
        NSLog(@"%f is the time difference",[date2 timeIntervalSinceDate:date1]);

    }

and this is the data that I get from server and I want the difference between start time and end time

2017-06-29 16:37:50.543 Barebones[4204:233648] requestReply cust category: {
date = "29-6-2017";
"end_time" = "17:45";
"market_crash" = true;
"start_time" = "14:28";
}

below is my nslog details

2017-06-29 16:37:50.543 Barebones[4204:233648] true
2017-06-29 16:37:53.482 Barebones[4204:233648] 14:28
2017-06-29 16:37:53.483 Barebones[4204:233648] 17:45
2017-06-29 16:37:53.483 Barebones[4204:233648] 29-6-2017
2017-06-29 16:37:58.493 Barebones[4204:233648] (null)
2017-06-29 16:37:58.494 Barebones[4204:233648] (null)
2017-06-29 16:45:47.514 Barebones[4204:233648] 0.000000 is the time difference

thanks in advance!!:)

Akshay
  • 179
  • 1
  • 16
  • Possible duplicate of [Difference between two NSDates](https://stackoverflow.com/questions/7147851/difference-between-two-nsdates) – Josh Heald Jun 29 '17 at 11:26
  • `date1` and `date2` are `null`. That won't work until you fix that. `dd/MM/YYYY hh:mm` DOESN'T match the string `29-6-2017 14:28`. Did you see the `/` on the string? No. Try with `dd-M-yyyy HH:mm` instead? – Larme Jun 29 '17 at 11:31

2 Answers2

3

Try below code

[date1 timeIntervalSinceReferenceDate] - [date2 timeIntervalSinceReferenceDate];

This will work :)

Neha Gupta
  • 539
  • 3
  • 13
2

The problem is in Your Date Formatter. I tried your code like this and it worked fine for me.

NSString * start = @"14:28";
NSString * end = @"17:45";
NSString * datefj = @"29-6-2017";

NSString *date1fj =[NSString stringWithFormat:@"%@ %@",datefj,start];
 NSString *date2fj =[NSString stringWithFormat:@"%@ %@",datefj,end];

NSDateFormatter *df=[[NSDateFormatter alloc] init];
// Set the date format according to your needs
//[df setDateFormat:@"dd-mm-yyyy hh:mm"]; //for 12 hour format
[df setDateFormat:@"dd-MM-YYYY HH:mm "];  // for 24 hour format

NSDate *date1 = [df dateFromString:date1fj];
NSDate *date2 = [df dateFromString:date2fj];
NSLog(@"%@",date1);
NSLog(@"%@",date2);
NSLog(@"%f is the time difference",[date2 timeIntervalSinceDate:date1]/60);

// Edit For Getting The time in Proper format

NSString *formattedTime = [self timeFormatted:[date2 timeIntervalSinceDate:date1]/60];

// Create a new function

- (NSString *)timeFormatted:(int)totalSeconds
{

    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
}
Nilesh Jha
  • 1,626
  • 19
  • 35