1

I have two times :

10:23:51
10:31:54

Both values are stored as strings in a SQLite database.

I want to find the difference between these two times in hh:mm format.
How can I do this?

I have searched a lot. But can't find a proper solution. Help me out. Thanks.

Edit : See basically I am having two arrays:
InTimeArray and OuttimeArray. Now in the viewDidload method , I am fetching all the intime entries in the IntimeArray and Outtime entries in the outtime Arrays.

Now I am passing one by one values in tableview like in 1st row

 IntimeArray[0]
 OuyTimeArray[0]

second row:

 IntimeArray[1]
 OuyTimeArray[1]

Now I want to count the differences between 1st row's intime and outtime in HH:MM format.
I am really confused.

Megha Mishty
  • 61
  • 2
  • 11

3 Answers3

1

you can take a look at that :

-(NSDate *)dateFromString:(NSString *)string
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:@"HH:mm:ss"];

NSDate *date1 = [dateFormat dateFromString:string];  
if(!date1) date1= [NSDate date];
[dateFormat release];
[locale release];

return date1;
}

this will turn the NSDate to you and when you change them both to NSDates then you can learn the difference by looking at this.

i hope this will help you..

Community
  • 1
  • 1
iremk
  • 677
  • 1
  • 5
  • 16
1

You have to first change your both strings into date format using this method :-

- (NSDate *)dateFromString:(NSString *)string;

after that you just have to calculate number of seconds between both the dates.Thats it.

OR there is another simple way to do this. i.e.

get dates from both arrays first like this:-

NSDate *eventStartDate = [IntimeArray objectAtIndex:indexpath.row];

NSDate *eventEndDate = [OuyTimeArray objectAtIndex:indexpath.row];

Then calculate difference between both the dates like this :-

NSTimeInterval startTimeInterval = [eventStartDate timeIntervalSinceDate:currentDate];

Hope it helps Thanks :)

Nikhil Bansal
  • 1,545
  • 13
  • 29
0

Try this:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"HH:mm:ss"];

NSDate *d1 = [formatter dateFromString:IntimeArray[0]];
NSDate *d2 = [formatter dateFromString:OuyTimeArray[0]];

NSTimeInterval ti = [d2 timeIntervalSinceDate:d1];
float hours = floor(ti / 3600);
float minutes = floor((ti - hours*3600) / 60);

NSString *s = [NSString stringWithFormat:@"%02.0f:%02.0f",hours,minutes];

For your given example, NSLog(@"Time passed: %@",s); will print 00:08.

tilo
  • 14,009
  • 6
  • 68
  • 85
  • hey I am putting my edited code referenced by you..it is giving me error. – Megha Mishty May 04 '12 at 13:33
  • If you get an error, then your `NSString` (which comes out of `IntimeArray[0]`) is not formatted correctly (i.e. "HH:mm:ss"). Make sure that the string has this format by printing it out via `NSLog()` before creating the date. – tilo May 04 '12 at 15:35
  • In my app it is must to have H:mm:ss format sir...If I will change this format than it will cause so many changes..Is there any way? – Megha Mishty May 05 '12 at 04:20
  • Please post the error that you're getting when running this code. – tilo May 05 '12 at 15:37