My server call gives me JSON data with a date time per piece of data and I want to calculate the time elapsed between now and then and load it into my data structure. The way I'm doing it now takes way to long, is there a different design practice I should be using? The follow function is what I am using right now
func dateDiff(_ dateStr:String) -> String {
var timeAgo = "10m"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
formatter.timeZone = NSTimeZone(name: "AST") as! TimeZone
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "AST") as! TimeZone
let now = formatter.string(from: Date())
if let date = formatter.date(from: dateStr){
if let nowDate = formatter.date(from: now){
let components = Calendar.current.dateComponents([.day,.hour,.minute,.second], from: date, to: nowDate)
let sec = components.second
let min = components.minute
let hours = components.hour
let days = components.day
if (sec! > 0){
if let secc = sec {
timeAgo = "\(secc)s"
}
}
if (min! > 0){
if let minn = min {
timeAgo = "\(minn)m"
} }
if(hours! > 0){
if let hourss = hours {
timeAgo = "\(hourss)h"
}
}
if(days! > 0){
if let dayss = days {
timeAgo = "\(dayss)d"
}
}
}
}
return timeAgo
}