Hi Team, I have been trying to display the difference between two dates in integer numbers only. This code works in SwiftUI playground. But in the SwiftUI ContentView it gives me an error. I have copied both sets of code below. Any suggestions?
//Swift Playground Code starts here: let theFirstDate = Date(timeIntervalSince1970: 5) let theSecondDate = Date() let theComponents = Calendar.current.dateComponents([.month], from: theFirstDate, to: theSecondDate)
let theNumberOfMonth = theComponents.month
let Months = theNumberOfMonth
//Swift Playground Code ends here // This code works perfectly // Displays the result as "Dec 31, 1969 at 6:00 PM" "Jul 15, 2022 at 6:05 PM" month: 630 isLeapMonth: false 630 630
I am looking to display the number 630 in the SwiftUI code below,
// SwiftUI Code starts here
import SwiftUI struct ContentView: View { @State var PurchaseDate = Date.now @State var Months: Int = 0
// Date picker. Here I select a date var body: some View { VStack(alignment: .leading){ DatePicker("Date of Purchase", selection: $PurchaseDate, in: ...Date(), displayedComponents: .date)
Spacer()
// Here we assign today's date let date = Date() let dateDiff = Calendar.current.dateComponents([.month], from: PurchaseDate, to: date)
let Months = dateDiff.month
// In the next line of code, I want to display months in integer, but I get the error Text("$(Months)") // On this line display error message is "No exact matches in call to instance method 'appendInterpolation'"
Spacer()
}
}
}