您好我正在使用下面的代码,但我得到null值startDate和endDate。请帮助我任何一个
-(NSString *)datecomparison:(NSString *)STARTDATE forEndDate:(NSString *)ENDDATE{
NSLog(@"startDate:%@ endDate:%@",STARTDATE,ENDDATE);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-yyyy"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:@"12/05/12"];
dt2 = [df dateFromString:@"13/05/12"];
NSLog(@"startDate:%@ endDate:%@",dt1,dt2);
NSComparisonResult result = [dt1 compare:dt2];
NSString *dateReturn = [[NSString alloc] init];
switch (result)
{
case NSOrderedAscending: NSLog(@"%@ is greater than %@", dt2, dt1);
dateReturn = @"ascending";
break;
case NSOrderedDescending: NSLog(@"%@ is less %@", dt2, dt1);
break;
case NSOrderedSame: NSLog(@"%@ is equal to %@", dt2, dt1);
dateReturn=@"ascending";
break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
return dateReturn;
}
答案 0 :(得分:0)
您可以从两个日期获得年,月,日并进行比较。
NSString *str = [dt1 description];
const char* szDate = [str UTF8String];
int nYear, nMonth, nDay, nHour, nMinite, nSecond, nDeltaHour, nDeltaMinite;
sscanf(szDate, "%04d-%02d-%02d %d:%02d:%02d +%02d%02d", &nYear, &nMonth, &nDay, &nHour, &nMinite, &nSecond, &nDeltaHour, &nDeltaMinite);