JessJ JessJ - 1 year ago 60
Swift Question

How to show ~year(s) ~month(s) ~day(s) has been passed using NSdate- swift IOS

I'm playing with swift. I want to show how many years/months/days has been passed since the specific date(NSdate).

If the specific date is 08/01/2016 and current date is 08/11/2016, it will show only "10day(s) has been passed". If the specific date is 08/11/2015, it will show "1year(s) 0 month(s) 0day(s)has been passed"

Answer Source

Do something like this. Use your dates in date1 and date2

let date1 = NSDate()
let date2 = NSDate()

let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Full
form.allowedUnits = [.Year, .Month, .Day]
let s = form.stringFromDate(date1, toDate: date2)