-2

I am building an IOS application where I am doing following step.

  1. Parsing the data.
  2. Getting OpenTime and CloseTime.
  3. Set the OpenTime and CloseTime in a NSString variable name as openTime and closeTime.

Now the problem is I want to calculate the different of this two time.

I am getting the value like, OpenTime = "00:15:00" and CloseTime = "1:30:00"

The Time formate is - 24 hours.

Can someone help me in solving this problem I am new in IOS.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What's the expected output? – Marcus Adams May 19 '15 at 13:57
  • Presumably your strings are being delivered from somewhere remote? If you're generating them on the device, just don't store them as strings. – Tommy May 19 '15 at 13:57
  • @Logan. Suppose NSString *openTime = @"00:15:00"; NSString *closeTime = @"1:30:00"; in this 2 string I have Time value and I need a different of this 2 value. – hitesh matnani May 19 '15 at 13:59
  • @Tommy in which dataType I should store and how to calculate the Time different – hitesh matnani May 19 '15 at 14:00
  • @MarcusAdams Suppose OpenTime is - "00:15:00" and closeTime is "00:45:00" then in this case the expected output should be "00:30:00". – hitesh matnani May 19 '15 at 14:02
  • possible duplicate of [Difference between two NSDate objects -- Result also a NSDate](http://stackoverflow.com/questions/5562594/difference-between-two-nsdate-objects-result-also-a-nsdate) – Pfitz May 19 '15 at 14:27

5 Answers5

1

Let say you have two NSDate variables openTime and closeTime

int calendarUnit = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *components = [[NSCalendar currentCalendar] components:calendarUnit fromDate: openTime   toDate: closeTime  options:0];

int months = [components month];
int days = [components day];
int hours = [components hour];
int minutes = [components minute];
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

You can use NSDateComponents

NSDate *date = [self getTimeInDateFormatFromString://your time string]; // your method return nsdate from string using nsformatter
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];

you can use comp.hour and minute to get time difference.

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
Shoaib
  • 21
  • 3
  • Thanks for Answer can you please explain your code in detail – hitesh matnani May 19 '15 at 14:08
  • the converted date will decompose to nsdatecomponents which will return you hour and minute in integer format. You can compare your two dates components to get your required result i.e difference b/w two dates. the following link is with code snippet : http://stackoverflow.com/a/30328363/4916336 – Shoaib May 19 '15 at 15:20
0
NSString *openTime = @"12:48:00";
NSString *closingTime = @"21:52:00";

NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"hh:mm:ss"];

NSDate *openDate = [formatter dateFromString:openTime];
NSDate *closeDate = [formatter dateFromString:closingTime];

int calendarUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *components = [[NSCalendar currentCalendar] components:calendarUnits fromDate: openDate toDate: closeDate options:0];

NSLog(@"The difference is: %ld hours , %ld minutes, %ld seconds", (long)components.hour, (long)components.minute, (long)components.second);
Teo
  • 3,394
  • 11
  • 43
  • 73
0
-(void)compareTwoDate:(NSDate *)start :(NSDate *)end
{
    NSDate *end_date =end;
    NSDate *startDate =start;

    NSTimeInterval distanceBetweenDates = [startDate    timeIntervalSinceDate:end_date];

    double secondsInMinute = 60;

    NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
}
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
Anoop
  • 409
  • 3
  • 13
  • You might want to tell the OP how to convert the date from string to `NSDate` object and also why you calculate the difference in minutes. – trojanfoe May 19 '15 at 14:45
  • @trojanfoe, Your start and end time are in NSString OR NSDate format ? – Anoop May 19 '15 at 14:48
  • The OPs dates are in string format. Also that method should probably `return` something. – trojanfoe May 19 '15 at 14:49
  • @trojanfoe, have posted the answer below.Here you will Get difference in seconds. Than you can convert in minutes OR Hours. – Anoop May 19 '15 at 14:56
0
NSArray *star_array=[@"00:15:00" componentsSeparatedByString:@":"];

NSInteger start_time=[[star_array objectAtIndex:2] integerValue]*3600+[[star_array objectAtIndex:1] integerValue]*60+[[star_array objectAtIndex:0] integerValue];

NSArray *end_array=[@"1:30:00" componentsSeparatedByString:@":"];
NSInteger end_time=[[end_array objectAtIndex:2] integerValue]*3600+[[end_array objectAtIndex:1] integerValue]*60+[[end_array objectAtIndex:0] integerValue];

NSInteger diff_seconds=end_time-start_time;
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
Anoop
  • 409
  • 3
  • 13