-1

Hi hope someone can help me. I need to calculate the days between to dates. The flow are:

  • Pick a date
  • New date by years (ex. 2 years)
  • Get the days between the "pick date" and the "New date"
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Flemming Ovesen
  • 305
  • 2
  • 4
  • 15

1 Answers1

1

Try this code, updated for 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)