11

Possible Duplicate:
How to calculate time in hours between two dates in iOS

I am beginner to Objective-C, so I need your help in syntax making.

My problem goes like this:

I have 2 Strings, and I want to convert that into NSDate and then calculate the time difference between the dates in minutes.

How can I do this?

Community
  • 1
  • 1
Sagar Mohanty
  • 227
  • 1
  • 3
  • 12
  • @EvanMulawski that answer is for Mac Os and not for iOS i think!! Since we don't have [NSDate dateWithString:@"....."] such thing in iOS https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsdate_Class/Reference/Reference.html – Bala Apr 29 '12 at 17:01
  • http://www.ios-developer.net/iphone-ipad-programmer/development/date-and-time/date-and-time-examples I hope this will lead you in right direction – Dinesh Raja Apr 29 '12 at 17:04
  • 1
    @Bala That is trivial. Just look at the related questions on the right and you can find iOS equivalents, like http://stackoverflow.com/questions/4575689/objective-c-calculating-the-number-of-days-between-two-dates. – Evan Mulawski Apr 29 '12 at 17:51

1 Answers1

35
  1. Use NSDateFormatter to convert the date strings into NSDate objects.
  2. Call -[NSDate timeIntervalSinceDate:] to get the difference between the two dates in seconds.
  3. Divide by 60 to get the difference in minutes.

Alternatively, you can use NSCalendar and NSDateComponents to calculate the difference in minutes for different calendars by using the components:fromDate:toDate:options: method.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • 1
    Here you go - (NSInteger)minutesBetween:(NSDate *)firstDate and:(NSDate *)secondDate { NSTimeInterval interval = [secondDate timeIntervalSinceDate:firstDate]; return (int)interval / 60; } – KamyFC Jun 13 '17 at 07:09