Task timeIntervalSinceDate

The last resort, because I cannot have my life working!

I set the date when my application closes (using applicationWillTerminate) by default user

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *timeClosed = [[NSDate alloc] init];
[defaults setObject: timeClosed forKey:@"svdTimeClosedApp"];

then when the application is running, I want to compare this time using

NSDate *timeSaved = svdTimeClosedApp;
NSDate *timeNow = [[NSDate alloc] init];
double timeInterval = [timeSaved timeIntervalSinceDate:timeNow];
NSLog(@"time now = %@, time saved = %@, time diff = %@", timeNow, timeSaved, [NSString stringWithFormat:@"%d",timeInterval]);

I tried to display this in a log window, expecting to see a beautifully formatted string for about 20 seconds. The problem is that it comes out as 2047868928!

Any ideas ?!

(log window output below)

time now = 2009-12-19 20:54:02 +0000, time saved = 2009-12-19 20:48:29 +0000, time diff = 2047868928

Thanks for any help!

+3
source share
3 answers

. -, stringWithFormat: % g, % d,% d . , [timeNow timeIntervalSinceDate:timeSaved], .

+10

, .

, , / :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:[NSDate date] forKey:@"LastSyncDate"];
[prefs synchronize];

, , , :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSDate* dateOfLastSync = [prefs objectForKey:@"LastSyncDate"];
if (dateOfLastSync != nil)
{
    NSDate *timeNow = [NSDate date];
    double SECONDS_TO_DAY = 60 * 60 * 24;
    int timeIntervalInDays = (int)([timeNow timeIntervalSinceDate:dateOfLastSync] / SECONDS_TO_DAY);
    NSLog(@"The last sync was %d days ago.", timeIntervalInDays);
}

, .

Mike

www.MikesKnowledgeBase.com

+2

, timeInterval , ? , , .

double timeInterval = [timeNow timeIntervalSinceDate:timeSaved];
0

Source: https://habr.com/ru/post/1726148/


All Articles