ios - NSDate 比较总是返回 NSOrderedSame

标签 ios compare nsdate

我对 NSDate 比较有疑问。我尝试了在谷歌搜索我的问题时发现的几种解决方案,但没有任何变化:我比较两个日期,我总是得到 NSOrderedSame 或 NSTimeIntervals = 0.0000 之间的差异;

我的代码是这样的:

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    for (checklisteModel *updateRecord in updateArray) 
    {

        NSDate *serverDate = [inputFormatter dateFromString:updateRecord.last_update];

        NSString *lastUpdateFromLocalRecord = [self getLastUpdateForChecklisteItemWithID:updateRecord.checklisteID];
        NSDate *localDate = [inputFormatter dateFromString:lastUpdateFromLocalRecord];

        NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
        NSLog(@"checkliste ID: %i",updateRecord.checklisteID);
        NSLog(@"server Date: ----- %@",[fmt stringFromDate:serverDate]);
        NSLog(@"local Date: ----- %@",lastUpdateFromLocalRecord);



        NSLog(@"difference of dates: %f",[serverDate timeIntervalSinceDate:localDate]);


        NSTimeInterval distanceBetweenDates = [serverDate timeIntervalSinceDate:localDate];
        double secondsInMinute = 60;
        NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;

        if (secondsBetweenDates == 0)
            NSLog(@"seconds between == 0");
        else if (secondsBetweenDates < 0)
            NSLog(@"seconds between < 0");
        else
            NSLog(@"seconds between > 0");



        //last_update on server is earlier than local
        if ([serverDate compare:localDate] == NSOrderedAscending) 
        {

            NSLog(@"Server data is earlier than local data");
            NSLog([NSString stringWithFormat:@"server data: %@, local data: %@",updateRecord.last_update,lastUpdateFromLocalRecord]);
        }
        // server data is later than local data
        else if ([serverDate compare:localDate] == NSOrderedDescending) 
        {

            NSLog(@"Server data is later than local data");
            NSLog([NSString stringWithFormat:@"server data: %@, local data: %@",updateRecord.last_update,lastUpdateFromLocalRecord]);
        }
        else 
        {
            NSLog(@"Server data and local data are the same");
        }
    }

我的部分调试显示了这一点:

checkliste ID: 21
server Date: ----- 
local Date: ----- 2012-10-29 10:13:46
difference of dates: 3810.000000
seconds between > 0
Server data is later than local data
server data: 2012-10-29 11:17:16, local data: 2012-10-29 10:13:46

最佳答案

NSLog(@"server Date: ----- %@",updateRecord.last_update);

您没有在此处记录 serverDate,因此正如 Hot Licks 所建议的,它很可能为 nil。在Objective-C中可以向nil发送消息,结果为0nil。你认为 NSOrderedSame 的值(value)是什么?我敢打赌它是 0

[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

我注意到您的日期格式化程序的输入格式与您记录的内容不完全匹配,因此预期输入与您提供的内容之间的差异可能足以阻止格式化程序创建日期。尝试为 inputFormatter 的日期格式添加秒数。

关于ios - NSDate 比较总是返回 NSOrderedSame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13120141/

相关文章:

iOS Swift 本地通知不是 "popping up"

oracle - 将日期类型的当前日期与 Oracle 中的 Varchar 日期列进行比较

python - 根据文件名比较目录

ios - 从日期中提取所有日期组件

objective-c - 如何创建 Cocoa GUI 来编辑 NSDate 对象数组?

ios - 启动 appium 时仪器崩溃

ios - 顶部带有 UIViews 的滚动 Collection View ?

ios - 初始化两个元组的数组失败

objective-c - 如何将 .hour() 和 .min() 从 Swift 转换为 Objective-C?

javascript - 如何比较不包括在单独数组中指定的字段的两个 JSON 对象?( postman 脚本)