1

I have two Date objects and I want to compare only the time difference between them, ignoring the date. Would be something like:

dt1 = (Date) 2016-09-22 20:05:01 UTC
dt2 = (Date) 2016-08-20 22:06:00 UTC

and the difference between dt1 and dt2 would be 2 hours or 121 minutes.

Is there a function in Swift that I could do that?

yasin
  • 1,297
  • 3
  • 17
  • 36

5 Answers5

6

You can use Calendar to extract the DateComponents and then combine the day, month, and year from one with the hour, minute, and second of the other into another Date object. Then you can compare the two Date objects that share the same date using the well documented methods.

For example, given date1 and date2, we can calculate date3 with date of date1, but time of date2. We can then compare date1 to this new date3:

let calendar = Calendar.current
let components2 = calendar.dateComponents([.hour, .minute, .second], from: date2)
let date3 = calendar.date(bySettingHour: components2.hour!, minute: components2.minute!, second: components2.second!, of: date1)!

Then, to get the number of minutes between date1 and date3:

let minutes = calendar.dateComponents([.minute], from: date1, to: date3).minute!

Or hours and minutes:

let difference = calendar.dateComponents([.hour, .minute], from: date1, to: date3)
let hours = difference.hour!
let minutes = difference.minute!

Or, if you want to show this to the user, you'd often use DateComponentsFormatter to get a nice, localized string representation:

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.allowedUnits = [.hour, .minute]  // or [.minute]
let string = formatter.string(from: date1, to: date3)!
Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

Ignoring the date, You can use timeIntervalSince(date:) to get the number of seconds between two dates.

0

Use NSCalenar, then find out the hour, minute land then find the difference between them.

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

Swift 3 Extention for comparing Time Only

import UIKit

extension Date {
    func compareTimeOnly(to: Date) -> ComparisonResult {
    let calendar = Calendar.current
    let components2 = calendar.dateComponents([.hour, .minute, .second], from: to)
    let date3 = calendar.date(bySettingHour: components2.hour!, minute: components2.minute!, second: components2.second!, of: self)!

    let seconds = calendar.dateComponents([.second], from: self, to: date3).second!
    if seconds == 0 {
        return .orderedSame
    } else if seconds > 0 {
        // Ascending means before
        return .orderedAscending
    } else {
        // Descending means after
        return .orderedDescending
    }
    }
}

Some Tests

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

let date1 = Date()
var date2 = dateFormatter.date(from: "2016-12-26T12:12:12")!

let result = date1.compareTimeOnly(to: date2)
print(result.rawValue)
Noor Ali Butt
  • 798
  • 8
  • 24
-1
func findsecond(s_date:String,e_date:String) -> Int {
    //set date formate

    let Dateformatter = DateFormatter()

    Dateformatter.dateFormat = "dd MMM yyyy h:mm a"

    //convert string to date

    let dateold = Dateformatter.date(from: start_date)!
    let datenew = Dateformatter.date(from: end_date)!
    //use default datecomponents with two dates
    let calendar1 = Calendar.current
    let components = calendar1.dateComponents([.second], from: dateold, to: datenew)
    let seconds = components.second
    print("Seconds: \(seconds)")
    return seconds!

}