0

How do I calculate date count difference between two days in objectice C formate.

GR.
  • 455
  • 1
  • 4
  • 18

3 Answers3

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
Andriy
  • 2,767
  • 2
  • 21
  • 29
Danny
  • 23
  • 1
  • 1
  • 6
  • 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