Hi, this is my function
I cannot figure out why i always get jan 1, 2000 [time...]
this is my code
if let thisTime = Int64(time) {
let date = Date(timeIntervalSince1970: TimeInterval(thisTime) / 1000)
let dateWithTimezone = convertStringToDateWithTimezone(date)
print(formatDate(dateWithTimezone, "hh:mm a")!)
}
The date variable is correct but once I use convertStringToDateWithTimezone the result is always jan 1, 2000 [time...] Any idea what could be wrong?
func convertStringToDateWithTimezone(_ d: Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: (dateFormatter.string(from: d)))!
}
func formatDate(_ date: Date?, _ pattern: String) -> String? {
if let date = date {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en")
dateFormatter.dateFormat = pattern
return dateFormatter.string(from: date)
}
return nil
}