ios - How can I display the time offset (years, months, weeks, ...) from two dates at my labels? -
i getting use of unresolved identifier 'self'
in app. after reading this link, wrapped code in class.
note: using extension got answer @ s.o.
class setdate { var seconds = date2.secondsfrom(date1) var minutes = date2.minutesfrom(date1) var hours = date2.hoursfrom(date1) var days = date2.daysfrom(date1) var weeks = date2.weeksfrom(date1) var months = date2.monthsfrom(date1) var years = date2.yearsfrom(date1) func setlabels(seconds,minute,hous,weeks,days,months,years) { self.slabel.text = s self.mlabel.text = min self.hlabel.text = h self.wlabel.text = w self.dlabel.text = d self.mlabel.text = m self.ylabel.text = y } setlabels(seconds,minutes,hours,weeks,days,months,years) }
how can resolve issue?
i have created new extension output offset components string 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 labels // // yearslabel.text = years // monthslabel.text = months // weekslabel.text = weeks // dayslabel.text = days // hourslabel.text = hours // minuteslabel.text = minutes // secondslabel.text = seconds } setlabels()
Comments
Post a Comment