Reputation: 3301
I have googled the answer for this question and not found any objective C code. I have a situation to calculate the end time from a start time.
For e.g.: consider now current date and time is 29th 10:00 AM, My question is what will be the time and date after 5 hrs 30 mins. I can't simply calculate using the selected hours because, at the end of the day, even date will vary in that case, I need to find out and get the proper date and time.
I have done the same on windows using some win32 API, just want to implement the same on IOS, If any one have tried like that before, please share your ideas.
thanks
Upvotes: 1
Views: 986
Reputation: 11276
Try this function
+ (NSDate* )date:(NSDate*)date ByAddingHours:(int)hours
{
return [date dateByAddingTimeInterval:60*60*hours];
}
Change int to float or something to support like 2.5 hours or 3.5 hours.
Upvotes: 0
Reputation: 46533
You need to convert the 5 hours 30 minutes to seconds and then add to the date.
NSDate *date = [NSDate date];
NSInteger minutes = 5*60+30;//5hrs 30min
NSDate *futureDate = [date dateByAddingTimeInterval:minutes*60];
Upvotes: 5