ios - Comparing Hours in Swift -
while querying data database receive hours process started , ended in 2 separate string fields example start = "1100" , end = "+0200" indicate it's hours of operation 11am-2am. proper way represent in swift can determine amount of time left current time end time of process.
edit: found interesting way using date formatter if remove possible prefix of + , use below code seems work correctly; however, date not set work around?
let dateformatter = nsdateformatter() dateformatter.dateformat = "hhmm" let date = dateformatter.datefromstring("1340")
you can create nsdate
components using nscalendar.currentcalendar().datewithera
(there other similar functions, nscalendar
details). need add logic determine if 2am today or tomorrow etc.
then can compare 2 nsdate
dates. determine time left end use nsdate
method timeintervalsincedate
. can use nsdatecomponentsformatter
remaining time nicely formatted.
Comments
Post a Comment