Objective-C midpoint between two

I have two NSDates formatted as "h: mm a" as time (ie 6:00 AM and 8:00 PM).

I am trying to figure out what time is the intermediate point between these two points.

In the above example, the midpoint between 6:00 and 20:00 will be 1:00 PM.

But I do not know how to do this in objective-C for iPhone sdk.

Any help or suggestions or code is appreciated.

+5
source share
2 answers

Perhaps something like the following: what are you after?

// Assuming you have two NSDate instances: start and end.
NSTimeInterval difference = [end timeIntervalSinceDate:start];
NSDate *middle = [NSDate dateWithTimeInterval:difference / 2 sinceDate:start];

See the NSDate class reference for more information .

+17
source

, , NSDate.

, NSDate, :

NSTimeInterval intA = [timeA timeIntervalSince1970];
NSTimeInterval intB = [timeB timeIntervalSince1970];

NSDate *avgTime = [NSDate dateWithTimeIntervalSince1970:(intA + intB) / 2.0];
+2

All Articles