Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring time Interval Since Now

anyone know or can provide some example code relating to "timeIntervalSinceNow" method...

I need something like... time2(when app eneters foreground) - time1(when app enters background) = time3(the difference in times)... this is so i can use this number(pref in seconds) to calculate the time i have lost while the app has been in background !!

I am having trying trying to create the date objects, receive the object and display/use in a label....

like image 352
myles Avatar asked Dec 28 '22 20:12

myles


2 Answers

Actually, to answer your original question, myles, you can use timeIntervalSinceNow. In the statement below, inputDate has been initialized as an NSDate and set to some date (you could just try [NSDate *inputDate = [NSDate date]; to set the date at the current date and time.

NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];

The next line is a way to put that NSTimeInterval into a string.

NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:@"%f",timeToAlert];

Finally, the app delegate class is typically where code can be written to handle coming in and out of background. Good luck!

like image 115
bob s Avatar answered Jan 11 '23 13:01

bob s


timeIntervalSinceNow tells you the offset of an NSDate from the current time. You want timeIntervalSinceDate::

NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;

NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];
like image 20
Jonathan Grynspan Avatar answered Jan 11 '23 14:01

Jonathan Grynspan