希望有人能帮助我。我需要计算日期之间的天数。流程是:
答案 0 :(得分:1)
试用此代码,针对Swift 2.0进行了更新
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let first = "2010-09-01"
let firstDate = dateFormatter.dateFromString(first)!
let last = "2010-09-05"
let lastDate = dateFormatter.dateFromString(last)!
let currentCalendar = NSCalendar.currentCalendar()
let timeUnitDay = NSCalendarUnit.Day
let daysBetween = currentCalendar.components(timeUnitDay, fromDate: firstDate, toDate: lastDate, options: NSCalendarOptions.MatchStrictly)
print(daysBetween.day)