How do I calculate date count difference between two days in objectice C formate.
Asked
Active
Viewed 372 times
3 Answers
2
Check if Installation Date is Registered or not, if registered the Calculate date otherwise register it.
if([[NSUserDefaults standardUserDefaults] objectForKey:@"IDate"])
{
NSDate *iDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"IDate"];
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:iDate];
int diff=interval/86400;//for converting seconds into days.
NSLog(@"%d Days",diff);
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"IDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

Dipen Panchasara
- 13,480
- 5
- 47
- 57
1
if you mean something like a "trial time" and after that you have to register: I would do as followed:
NSDate *installDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"InstallationDate"];
if(installDate)
{
// time in Seconds
long int timePassed = [installDate timeIntervalSinceNow] * -1;
NSLog(@"%ld", timePassed);
// now do your calculations and compare
}
else // first start
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"InstallationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"InstallationDate"]);
}
(as addition to Danny's post)

geo
- 1,781
- 1
- 18
- 30
0
Store your device installed date in NSUserDefaults
once its first run and then calculate that with the current date.
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // in seconds
-
Note that this doesn't take into account time changes, leap years, etc. You should not calculate "days" between two dates this way. – lnafziger Apr 23 '13 at 15:28