ios - UIDatePicker minus the exact time - Swift -
ios - UIDatePicker minus the exact time - Swift -
i'm making alarm clock tell how many hours , minutes of sleep get. set uidatepicker
user chooses time wanna wake up. tells exact time second. part i'm stuck on how many hours of sleep going get. tried subtracting exact time uidatepicker
. worked if both in am. illustration if user wanted wake @ 10:30 , 9:30 have subtract 10:30 9:30 1 hour. realized wouldn't work if different time of days e.g. or pm.
how got time uidatepicker
func handler(sender: uidatepicker) { var timeformatter = nsdateformatter() timeformatter.timestyle = nsdateformatterstyle.shortstyle var strdate = timeformatter.stringfromdate(thedatepicker.date) } thedatepicker.addtarget(self, action: selector("handler:"), forcontrolevents: uicontrolevents.valuechanged)
how got exact time
var date = nsdate() var outputformat = nsdateformatter() outputformat.locale = nslocale(localeidentifier:"en_us") outputformat.dateformat = "hh:mm:ss" timelabel.text = (outputformat.stringfromdate(date)) var timer = nstimer.scheduledtimerwithtimeinterval(1.0, target: self, selector: selector("gettime"), userinfo: nil, repeats: true)
my question:
how subtract uidatepicker
exact time hours of sleep user getting?
here illustration swift playground:
// setting date since don't have uidatepicker allow datestring = "2014-11-12 07:25" allow dateformatter: nsdateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd hh:mm" var wakeuptime: nsdate = dateformatter.datefromstring(datestring)! // var wakeuptime: nsdate = thedatepicker.date allow fromdate = nsdate() allow gregoriancalendar: nscalendar = nscalendar(calendaridentifier: nsgregoriancalendar)! allow flags: nscalendarunit = .hourcalendarunit | .minutecalendarunit allow components = gregoriancalendar.components(flags, fromdate: fromdate, todate: wakeuptime, options: nscalendaroptions(0)) println("\(components.hour) hours, \(components.minute) minutes")
ios swift nsdate nsdatepicker
Comments
Post a Comment