2012-10-29 81 views
1

我有NSDate比较问题。我尝试了几个解决方案,但是在搜索问题时发现了一些问题,但没有任何变化:我比较两个日期,并且总是得到NSOrderedSame或NSTimeIntervals = 0.0000;NSDate比较总是返回NSOrderedSame

我的代码如下所示:

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 
+0

你可以发布你如何初始化'NSDateFormatter'?我相信问题应该在那里。 – atxe

+1

最有可能的一个或两个你的NSDate对象是零。请注意,您不记录'serverDate'。 –

+0

我已经为你添加了dateformatter。 @Hot Licks:服务器日期在NSLog中,查看底部调试屏幕中服务器​​的日期与当地日期的不同。 – Wenduros

回答

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

你不记录serverDate这里,这样热舔建议,有很可能是零。您可以在Objective-C中将消息发送至nil,结果为0nil。你认为NSOrderedSame的价值是什么?我会下注一美元,它是0

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

我注意到,你的日期格式的输入格式不完全匹配你记录什么,所以也许预期的输入和你提供的是足以防止格式化从创建日期之间的区别。尝试在inputFormatter的日期格式中添加秒。

+1

...有人欠你一美元 –

+0

向格式化程序添加秒钟帮助至少日期之间的差异以秒/分钟为单位。这帮助我很多,因为我只是想检查日期是否低于或超过本地数据时间。所以我只会比较“secondsBetweenDates”是否小于,甚至大于0。 – Wenduros