IOS 如何比较一个日期和另一个日期?

IOS how to compare date with another date?

我有一个包含 XML 数据列表的 Web 服务。其中一个元素是 "UpdateTimeStampGMT"。我已经在这个元素中检索了这个参数,当我注销它进行检查时它看起来像这样。

 UpdateTimestampGMT = "2015-11-03T01:00:59.503";

但现在的问题是我想利用这个日期来检查这个日期是 4 天前的日期还是 3 天前的日期,如果它有效我想检索其他元素属于正确日期的区块。

编码如下:

   int minusThreeDayopenSSLCount = 0;

    int bluecolor = 0;
    NSDate *now = [NSDate date];
    NSDate *threeDaysAgo = [now dateByAddingTimeInterval:-3*24*60*60];
//This is the data when i NSLOG threeDaysAgo: 2015-10-29 09:06:40 +0000

for (NSDictionary *feed in feeds) {
        if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"] && [feed[@"UpdateTimestampGMT"] isEqualToString:threeDaysAgo]) {
            minusThreeDayopenSSLCount ++;
        }
    }
 NSLog(@"-3 Total openSSLCount count is (green):  %d",minusThreeDayopenSSLCount);

当我NSLOG

minusThreeDayOpenSSLCount看起来是这样的,这是不正确的

--> 2015-11-05 17:06:44.443 FYP_IOS_APP[928:444918] -3 Total openSSLCount count is (green): 0

当我改变时

if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"] && [feed[@"UpdateTimestampGMT"] isEqualToString:threeDaysAgo]) {
//            minusThreeDayopenSSLCount++;
//        }

if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"] && [feed[@"UpdateTimestampGMT"] isEqualToDate:[NSDate date]]) {
            openSSLCount++;
        }

它给我这个错误:

2015-11-05 17:38:08.138 FYP_IOS_APP[938:448845] -[__NSCFString isEqualToDate:]: 无法识别的选择器发送到实例 0x15cd90340 2015-11-05 17:38:08.139 IOS_APP[938:448845] *** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[__NSCFString isEqualToDate:] : 无法识别的选择器发送到实例 0x15cd90340'

你可以这样做:

NSDate *now = [NSDate date];
NSDate *threeDaysAgo = [now dateByAddingTimeInterval:-3*24*60*60];

NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

NSString *threeDaysAgoAsAString = [df stringFromDate:threeDaysAgo];

for (NSDictionary *feed in feeds) {
  if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"] && [feed[@"UpdateTimestampGMT"] isEqualToString:threeDaysAgoAsAString]) {
    minusThreeDayopenSSLCount ++;
  }
}

总体而言,我认为您不应该通过比较字符串来检查日期是否相等,因为这意味着两个日期值必须完全相同(例如“2015-11-03T01:00:59.503" != "2015-11-03T01:00:59.502")...

更新

试试这个:

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [NSDateComponents new];
components.day = -3; // days to subtract

NSDate *now = [NSDate date];
NSDate *threeDaysAgo = [calendar dateByAddingComponents:components toDate:now options:0];

NSUInteger threeDaysAgoComparisonValue = [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:threeDaysAgo];

NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

for (NSDictionary *feed in feeds) {
  if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"]) {
    NSDate *updateTimestampGMT = [df dateFromString:feed[@"UpdateTimestampGMT"]];
    NSUInteger dateComparisonValue = [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:updateTimestampGMT];
    if (dateComparisonValue == threeDaysAgoComparisonValue) {
      minusThreeDayopenSSLCount++;
    }
  }
}



另一个更新

如果您的目标是 >= iOS 8,您可以获得更简单的 threeDaysAgo 日期:

NSDate *threeDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-3 toDate:[NSDate date] options:0];



希望最终编辑:)

NSCalendar *calender = [NSCalendar currentCalendar];
NSDate *threeDaysAgo = [calender dateByAddingUnit:NSCalendarUnitDay value:-3 toDate:[NSDate date] options:0];

NSDateFormatter *df = [NSDateFormatter new]; // to create the dates in the for loop
df.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";

for (NSDictionary *feed in feeds) {
  if ([feed[@"Classification"] isEqualToString:@"Activity Summary - OpenSSL \"HeartBleed\" Scanning"]) {
    NSDate *updateTimestampGMT = [df dateFromString:feed[@"UpdateTimestampGMT"]];
    if ([calender isDate:updateTimestampGMT inSameDayAsDate:threeDaysAgo]) {
      minusThreeDayopenSSLCount++;
    }
  }
}