stumped stumped - 1 year ago 252
Swift Question

Getting time difference in Swift 3

I was trying to get the time difference of two dates from this guide. I'm not sure what to put in for the

argument. When I directly pass in
[.hour, .minute]
, I get an error:

Playground execution failed: error: MyPlayground.playground:10:46: error: type of expression is ambiguous without more context
let timeDifference = calendar.dateComponents([.hour, .minute], from: stopTime!, to: date!)


I tried to declare it as a constant first but am unsure what the type is. The argument is of type
, but I'm getting an error when I try to say it's of up
, or


let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "hh:mm a "
let time = "12:03 pm"
let stopTime = time)

let date = Date()
let calendar = Calendar.current

let components:[DateComponent] = [.hour, .minute]
let timeDifference = calendar.dateComponents(components, from: stopTime!, to: date!)

Answer Source

The dateComponents(_:from:to:) method is declared as follows:

public func dateComponents(_ components: Set<Calendar.Component>, 
    from start: Date, to end: Date) -> DateComponents

The first parameter is of type Set<Calendar.Components> but you seem to provide a [DateComponents]. That's why you see the error.

Therefore, it should be done this way:

let components:[Calendar.Component] = [.hour, .minute]
let timeDifference = calendar.dateComponents(Set<Calendar.Component>(components), 
    from: stopTime!, to: date)

First, I changed the array's type to be [Calendar.Component]. Second, I created a Set<Calendar.Component> before passing components to the method. Another thing is that you wrote date!. The ! is redundant because date already is a non-optional. No need to unwrap!

Alternatively, you can remove the components variable entirely!

let timeDifference = calendar.dateComponents([.hour, .minute], 
    from: stopTime!, to: date)

Now it compiles fine!

However, this might not produce your expected result because time) produces a date in the year 2000. I got this quite dirty solution, but it works anyway:

let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "hh:mm a"
let time = "08:00 pm"
var stopTime = time)

let date = Date()
let calendar = Calendar.current

let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let hour = calendar.component(.hour, from: stopTime!)
let minute = calendar.component(.minute, from: stopTime!)

stopTime = .year, value: year, of: stopTime!)
stopTime = .month, value: month, of: stopTime!)
stopTime = .day, value: day, of: stopTime!)
stopTime = .hour, value: hour, of: stopTime!)
stopTime = .minute, value: minute, of: stopTime!)

let timeDifference = calendar.dateComponents([.hour, .minute], from: stopTime!, to: date)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download