public func dateInterval(startTime:String) -> Int{
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"// 自定义时间格式
let endTime = newDateStr()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let date1 = dateFormatter.date(from: startTime),
let date2 = dateFormatter.date(from: endTime) else {
return -1
}
let components = NSCalendar.current.dateComponents([.second], from: date1, to: date2)
//如果需要返回月份间隔,分钟间隔等等,只需要在dateComponents第一个参数后面加上相应的参数即可,示例如下:
// let components = NSCalendar.current.dateComponents([.month,.day,.hour,.minute], from: date1, to: date2)
// print(components.month!,components.day!,components.hour!,components.minute!)
return components.second!
}
网友评论