I can't convert from TimeInterval to String with func and parameter on output.
It gives me this error message: Cannot convert value of type 'TimeInterval' (aka 'Double') to expected argument type 'Date'
import Foundation
extension Date {
func timeIntervalToString(date: TimeInterval) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: date)
return dateString
}
}
DateFormatter.string(from:) receives a Date
as an input parameter not a TimeInterval
.
Your TimeInterval
needs to be in a form like 1716889787
. It represents the number of seconds since 01/01/1970.
I believe it should be:
func timeIntervalToString(date: TimeInterval) -> String {
let date = Date(timeIntervalSince1970: date) //<- here
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: date)
return dateString
}