I have set a NSTimer.scheduledTimerWithTimeInterval
method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?
Thanks
You have access to a NSTimer's
fireDate
, which tells you when the timer is going to fire again.The difference between now and the
fireDate
is an interval you can calculate using NSDate'stimeIntervalSinceDate
API.E.G. something like:
When using the Solution of @Michael Dautermann with normal Swift type
Timer
andDate
I have noticed, that his solution will give you a negative value e.g.:When you insert a negative value in the
Timer.scheduledTimer(timeInterval:,target:,selector:,userInfo:,repeats:)
function it would lead to a timer set to 0.1 milliseconds as the Documentation of theTimer
mentions it.So in my case I had to use the negative value of the
nowDate.timeIntervalSinceDate(fireDate)
result:-nowDate.timeIntervalSinceDate(fireDate)