0

こんにちは、以下のコードを使用していますが、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;
}
4

1 に答える 1

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);
于 2012-10-19T12:13:29.590 に答える