tags:

views:

44

answers:

1

Hi guys, having trouble with this. i need to come up with a logic code.

How do i, given 2 set of times. subtract one from the other and change it all to seconds.

Lets say i have '12:00:00' and '15:00:00' Now i want to subtract 12:00:00 from 15:00:00 so the result will be 03:00:00. Then change it all to seconds so that will be 10800 seconds

i already have two sets of time in an NSString after being Formatted with a NSDateFormatter

+2  A: 

This is what you need to look for : NSDate -(NSTimeInterval)timeIntervalSinceDate

Convert your string to an NSDate and call this method [thisDate timeIntervalSinceDate:thatDate] will return you an NSTimeInterval, type double

vodkhang
hi . thx for replying. I need some help with converting the NSString to an NSDate. could u share some sample code?
Kenneth
ah ok nvm i found the dateFromString:String method
Kenneth
ah i have another problem, how do i assign it to a variable? sorry for asking but im new to obj-c
Kenneth
What is "it"? You mean the NSTimeInterval? It is not an object, you have to use NSTimeInterval time = [thisDate timeIntervalSinceDate:thatDate]; or you can use double. But I prefer NSTimeInterval
vodkhang
im getting null or 0 when NSLogged. here is the code that im using
Kenneth
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd/MM/yyyy"]; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"HH:mm:ss"]; NSDate *now = [[NSDate alloc] initWithTimeIntervalSinceNow:-3*60*60];NSDate *now2 = [[NSDate alloc] init];
Kenneth
NSString *theTime = [timeFormat stringFromDate:now]; NSString *theTimeNOW = [timeFormat stringFromDate:now2]; NSDate *cDate1 = [timeFormat dateFromString:theTime]; NSDate *cDate2 = [timeFormat dateFromString:theTimeNOW]; NSTimeInterval time = [cDate2 timeIntervalSinceDate:cDate1]; NSLog(@"time IN COREPLOT VIEW ~~~CONTROLLER~~ %@",time);
Kenneth
No, it is not an object, for sure, use %f for double. I mean this line : NSLog(@"time IN COREPLOT VIEW ~~~CONTROLLER~~ %@",time);
vodkhang
And then, why do you need to convert to NSString just to convert back to NSDate?
vodkhang
oh cool. alright . Thanks . got it working now.
Kenneth
oh yeah . good point. haha , guess i was thinking too much about it that i didn't noticed that.
Kenneth