0

如何计算objectice C甲酸盐中两天之间的日期计数差异。

4

3 回答 3

2

检查安装日期是否已注册,如果已注册则计算日期,否则请注册。

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];
}
于 2013-04-23T15:56:02.543 回答
1

如果您的意思是“试用时间”,然后您必须注册:我会按照以下方式进行:

    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"]);
    }

(作为丹尼帖子的补充)

于 2013-04-23T14:55:22.843 回答
0

将您的设备安装日期存储在NSUserDefaults第一次运行时,然后使用当前日期计算。

NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // in seconds
于 2013-04-23T14:33:21.150 回答