I have created a new extension to output the offset components as string for you:
import UIKit
extension NSDate {
func yearsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: date, toDate: self, options: nil).year
}
func monthsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitMonth, fromDate: date, toDate: self, options: nil).month
}
func weeksFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitWeekOfYear, fromDate: date, toDate: self, options: nil).weekOfYear
}
func daysFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitDay, fromDate: date, toDate: self, options: nil).day
}
func hoursFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitHour, fromDate: date, toDate: self, options: nil).hour
}
func minutesFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitMinute, fromDate: date, toDate: self, options: nil).minute
}
func secondsFrom(date:NSDate) -> Int{
return NSCalendar.currentCalendar().components(.CalendarUnitSecond, fromDate: date, toDate: self, options: nil).second
}
func offsetComponentsFrom(date:NSDate) -> (years:String,months:String,weeks:String,days:String,hours:String,minutes:String,seconds:String) {
let yearsFromDate = yearsFrom(date)
let years = "\(yearsFromDate) year" + {return yearsFromDate > 1 ? "s" : ""}()
let monthsFromDate = monthsFrom(date)
let months = "\(monthsFromDate) month" + {return monthsFromDate > 1 ? "s" : ""}()
let weeksFromDate = weeksFrom(date)
let weeks = "\(weeksFromDate) week" + {return weeksFromDate > 1 ? "s" : ""}()
let daysFromDate = daysFrom(date)
let days = "\(daysFromDate) day" + {return daysFromDate > 1 ? "s" : ""}()
let hoursFromDate = hoursFrom(date)
let hours = "\(hoursFromDate) hour" + {return hoursFromDate > 1 ? "s" : ""}()
let minutesFromDate = minutesFrom(date)
let minutes = "\(minutesFromDate) minute" + {return minutesFromDate > 1 ? "s" : ""}()
let secondsFromDate = secondsFrom(date)
let seconds = "\(secondsFromDate) second" + {return secondsFromDate > 1 ? "s" : ""}()
return (years,months,weeks,days, hours, minutes, seconds)
}
}
Testing
let date1 = NSCalendar.currentCalendar().dateWithEra(1, year: 2014, month: 11, day: 28, hour: 5, minute: 9, second: 0, nanosecond: 0)!
let date2 = NSCalendar.currentCalendar().dateWithEra(1, year: 2015, month: 8, day: 28, hour: 5, minute: 9, second: 0, nanosecond: 0)!
func setLabels() {
let components = date2.offsetComponentsFrom(date1)
let years = components.years // "0 year"
let months = components.months // "9 months"
let weeks = components.weeks // "39 weeks"
let days = components.days // "273 days"
let hours = components.hours // "6553 hours"
let minutes = components.minutes // "393180 minutes"
let seconds = components.seconds // "23590800 seconds"
// set your labels
//
// yearsLabel.text = years
// monthsLabel.text = months
// weeksLabel.text = weeks
// daysLabel.text = days
// hoursLabel.text = hours
// minutesLabel.text = minutes
// secondsLabel.text = seconds
}
setLabels()