//后台给的时间
var createDate = ""
//转化完的时间轴文字
var timeTips = ""
func checkTimes(){
let oldF = DateFormatter()
oldF.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let oldDate = oldF.date(from:createDate) else {return}
let timeDiff =Date().timeIntervalSince(oldDate)
if timeDiff/60<1{
timeTips="刚刚"
return
}
let mins = Int(timeDiff/60)
if mins<60{
timeTips="\(mins)分钟前"
return
}
let hours = Int(timeDiff/3600)
if hours<24{
timeTips="\(hours)小时前"
return
}
let days = Int(timeDiff/3600/24)
if days<30{
timeTips="\(days)天前"
return
}
let months = Int(timeDiff/3600/24/30)
if months<12{
timeTips="\(months)月前"
return
}
let years = Int(timeDiff/3600/24/30/12)
timeTips="\(years)年前"
}
网友评论