0

I have 2 NSDate object thats shows 24hour time. Is there a way I can subtract those two times and put the result in a label. Ive searched for a method but couldn't find one. The only thing I could find was cover it to a double with timeIntervalFromDate method, which returns a double and I need the 24hour representation of that.

user3275730
  • 25
  • 1
  • 6

3 Answers3

1

It just returns an NSTimeInterval that is typedef double NSTimeInterval is a representation in seconds.
Doing the math to know how may hours are in x seconds, you just need to divide the retuned interval to 3600 seconds and round the result.
I've build a simple extension to be used in swift to help in calendrical calculation, you can find it here: AFSwiftDateExtension

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

This function may solve your problem:

func hoursFrom(earlierDate : NSDate, laterDate:NSDate) -> Int
{
        return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitHour, fromDate: earlierDate, toDate: laterDate, options: nil).hour
}

Adapted from this related question.

Community
  • 1
  • 1
0
extension NSTimeInterval {
    var time:String {
        return String(format:"%02d:%02d:%02d", Int((self/3600.0)%24), Int((self/60.0)%60), Int((self)%60))
    }
}

println(30.time)    // "00:00:30"
println(60.time)    // "00:01:00"
println(600.time)   // "00:10:00"
println(3600.time)  // "01:00:00"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571