我想从数据库中计算现在和日期之间的时间差。
“Fri Oct 13 2017 15:14:27 GMT+0200 (CEST)”的正确 dateFormatter.dateFormat 是什么?
这是我的代码试用版..
http://userguide.icu-project.org/formatparse/datetime
let input = "Fri Oct 13 2017 15:14:27 GMT+0200 (CEST)"
let dateFormatter = DateFormatter()
var userCalendar = Calendar.current
userCalendar.timeZone = TimeZone.current
let requestedComponent: Set<Calendar.Component> = [.hour,.minute,.second]
dateFormatter.dateFormat = "EEE MMM dd yyyy kk:mm:ss"
let startTime = Date()
let endTime = dateFormatter.date(from: input)
let timeDifference = userCalendar.dateComponents(requestedComponent, from: endTime!, to: startTime)
print(timeDifference)
let date = Calendar(identifier: .gregorian).date(from: timeDifference)
dateFormatter.dateFormat = "HH:mm:ss"
let dateString = dateFormatter.string(from: date!)
print(dateString)
多数民众赞成在工作:
dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss"
let endTime = dateFormatter.date(from: "14/10/17 11:00:00")