我有一个包含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总openSSLCount计数为(绿色):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'
答案 0 :(得分:2)
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];
HOPEFULLY FINAL EDIT:)
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++;
}
}
}