ios - How to create a timer (chrono) with NSDateFormatter? -
this regular timer (chrono) minutes , seconds. works fine there way have same result nsdateformatter.
var min = 0 var sec = 0 func stringfromtimeinterval(interval: nstimeinterval) -> string { let interval = int(interval) let seconds = interval % 60 let minutes = (interval / 60) return string(format: "%2d:%02d",minutes, seconds) } func updatetimer(){ sec++ self.timeelapsed.text = self.stringfromtimeinterval(double(sec)) }
here attempt :
var sec = 0 func thetime(interval:nstimeinterval) -> string { var dateformatter = nsdateformatter() var date = nsdate(timeintervalsince1970: nstimeinterval()) dateformatter.dateformat = "mm:ss" return dateformatter.stringfromdate(date) } func updatethetime(){ sec++ self.newtimer.text = self.thetime(double(sec)) }
- i'm doing wrong because 00:00.
- how can make work?
you can use nsdate , nsdateformatter time string. create object of nsdateformatter , nsdate, set style of time nsdateformmatter, call stringfromdate time string.
var dateformatter = nsdateformatter() var date = nsdate(timeintervalsince1970: interval) dateformatter.dateformat = "mm-ss" dateformatter.stringfromdate(date)
Comments
Post a Comment