-3

I have 2 dates. I don't care about the date portion, just the time.

How can I compare 2 dates and get the timeinterval between 2 dates?

Should I set the dates to 01-01-2000 and leave the time alone to compare?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jason Jardim
  • 15
  • 1
  • 2

3 Answers3

0

Use DateComponents and get the hour, minute, and second of the two dates. At this point you have to assume a full 24 hour, 86400 seconds per day. There's no need to worry about daylight saving or leap seconds or anything since you are doing date independent calculations.

Convert the hours, minutes, and seconds into total seconds of the day for the two dates. Then simply subtract the two totals and you have the difference.

Here's a helpful Date extension:

extension Date {
    func secondsSinceMidnight() -> TimeInterval {
        let comps = Calendar.current.dateComponents([.hour,.minute,.second], from: self)

        return TimeInterval(comps.hour! * 3600 + comps.minute! * 60 + comps.second!)
    }

    func timeDifference(to date: Date) -> TimeInterval {
        return date.secondsSinceMidnight() - self.secondsSinceMidnight()
    }
}

Call timeDifference(to:) using your two dates and you will get the difference in seconds ignoring the date portion of the dates.

A negative result means that the to date is closer to midnight.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

If you have two dates you can use the method timeIntervalSince(Date).

For instance:

func calculateElapsedTime(from someTime: Date) -> TimeInterval {
    
    let currentTime = Date()
    
    var elapsedTime = currentTime.timeIntervalSince(someTime)
    
    return elapsedTime

}

If you only want to consider the time difference between the two dates, you first have to normalize the date. This can be done in the following cumbersome way:

let currentDate = Date()
let anotherDate = Date(timeInterval: 60, since: currentDate)
let formatter = DateFormatter()

formatter.timeStyle = .short

let currentTime = formatter.string(from: currentDate)
let anotherTime = formatter.string(from: anotherDate)

let currentIntervalTime = formatter.date(from: currentTime)
let anotherIntervalTime = formatter.date(from: anotherTime)

let elapsedTime = anotherIntervalTime?.timeIntervalSince(currentIntervalTime!)
halfer
  • 19,824
  • 17
  • 99
  • 186
MacUserT
  • 1,760
  • 2
  • 18
  • 30
  • Doesn't this also calculate based on the date too? For example if date 1 is 06-15-2019 12:00:00 and date 2 is 06-14-2019 12:01:00 - I'm looking for the result of timeInterval of 60 since its only 1 minute difference in TIME only. Make sense? – Jason Jardim Jul 16 '19 at 18:01
  • This method calculates the entire time. That means if there is a day difference, the time interval is 24 hours in seconds. I understand your question and I can't judge if it makes sense. – MacUserT Jul 16 '19 at 18:58
0

This is an alternative to rmaddy's solution completely based on DateComponents

extension Date {
    func timeComponents() -> DateComponents {
        return Calendar.current.dateComponents([.hour,.minute,.second], from: self)
    }

    func timeDifference(to date: Date) -> Int {
        return Calendar.current.dateComponents([.second], from: date.timeComponents(), to: self.timeComponents()).second!
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361