我如何计算两个日期之间的天数?
How can i calculate the days between two dates?
嗨希望有人能帮助我。我需要计算日期之间的天数。流量是:
- 选择一个日期
- 新的年份日期(例如 2 年)
- 获取 "pick date" 和 "New date"
之间的天数
试试这个代码,为 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)
嗨希望有人能帮助我。我需要计算日期之间的天数。流量是:
- 选择一个日期
- 新的年份日期(例如 2 年)
- 获取 "pick date" 和 "New date" 之间的天数
试试这个代码,为 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)