UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Working with dates: Error " Binary operator '+' cannot be applied to operands of type 'Date' and 'Int' "

Forums > Swift

The following code works:

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65

var retireDate: Date {
    let retireDate = birthdate + (65 * 31536000)
    return retireDate
}
retireDate

When I substitute "65" with "retireAge" witin the retireDate computed property like so....

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65

var retireDate: Date {
    let retireDate = birthdate + (retireAge * 31536000)
    return retireDate
}
retireDate

...I get the following error: "Binary operator '+' cannot be applied to operands of type 'Date' and 'Int'"

I'm not sure why this error occurs or how I can work around it.

2      

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65

birthDate is a Date because you initialized it with a Date init.

You have initialized retireAge with an Int literal so it is an Int.

You cannot add a Date and an Int together, as they are different types.

var retireDate: Date {
    let retireDate = birthdate + (retireAge * 31536000)
    return retireDate
}

Don't do date math like this. Use actual functions supplied by the system.

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65
let retireDate = Calendar.current.date(byAdding: .year, value: retireAge, to: birthdate)!

3      

Thought @roosterboy would get to this. The way you are doing it does not take in the account leap years etc so you are always better using the Date functions.

But I suppose you asking also why when you changed 65 to retireAge, why 65 worked but retireAge did not!

It because that the systems infered 65 as the first was date. it the same as

var double = 2.0
var int = 1

let answer1 = double + 1
let answer2 = double + int // <- this does not work

3      

Specifically, in the first case:

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65

var retireDate: Date {
    let retireDate = birthdate + (65 * 31536000)
    return retireDate
}
retireDate

Swift's Date type has an operator + which works on a Date and a TimeInterval. And TimeInterval is just a typealias (i.e., another name) for a Double. 65 and 31536000 are inferred by the compiler to be TimeIntervals (a.k.a. Doubles, technically as 65.0 and 31536000.0). So adding a Date and a TimeInterval (a.k.a. Double) works.

Essentially, you have:

Date + (TimeInterval * TimeInterval)

But in the second case:

let birthdate = Date(timeIntervalSince1970: 0)
let retireAge = 65

var retireDate: Date {
    let retireDate = birthdate + (retireAge * 31536000)
    return retireDate
}
retireDate

You declare retireAge as an Int using the literal 65. So using it in the date math line won't work because what you have is:

Date + (Int * TimeInterval)

and you just can't mix types like that (at least not in this case).

3      

Thank you! All three of your responses were extraordinarily helpful!

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.