2012-12-03 5 views
-1

두 날짜를 비교하고 싶습니다. 두 문자열 (fromDates 및 toDates)로 날짜를 비교해야합니다.NSString으로 NSString을 변환하면 iPhone에서 잘못된 값과 형식이 표시됩니다.

여기에서 날짜는 콘솔에 올바른 값을 표시합니다.

//Console. 
19/12/2012 from date is 

NSDateFormatter 프로세스가 잘못된 형식과 날짜를 가져오고 언젠가는 NULL을 표시합니다.

//console 
First = (null) 

나는 아래의 방법을 사용했다.

if (fromDate) { 
     self.printFromDate.text = [NSString stringWithFormat:@"%@",curBtn.dateString]; 
     fromDates = self.printFromDate.text; 
     NSLog(@"%@ from date is",fromDates); 
     dtFormatter = [[NSDateFormatter alloc] init]; 
     [dtFormatter setDateFormat:@"yyyy-MM-dd"]; 
     NSDate *strDate = [dtFormatter dateFromString:fromDates]; 
     NSLog(@"First = %@",strDate);  
    } 

아무도 나에게 어디로 가고 있는지 말해 줄 수 있습니까? wrong.i 완전하게 여기에 붙어 있습니다. 미리 감사드립니다.

+0

stringDate 및 NSDateFormatter의 dateformat은 날짜로 변환 할 때 동일한 형식이어야합니다. –

+0

귀하의 날짜 형식이 –

+0

이 될 것이라고 생각하지 마십시오.이 질문에 대한 답변 http://stackoverflow.com/questions/949416/how-to-compare-two-dates-in-objective-c –

답변

3

날짜 포맷터가 문자열 형식을 따르지 않습니다.

난 당신이 찾고있는 생각 :

[dtFormatter setDateFormat:@"dd/MM/yyyy"]; 
NSDate *date1 = [dtFormatter dateFromString:fromDates]; 
NSDate *date2 = [dtFormatter dateFromString:anotherDate]; 

if ([date1 compare:date2] == NSOrderedDescending) 
    NSLog(@"date1 comes first");   
else if ([date1 compare:date2] == NSOrderedAscending) 
    NSLog(@"date2 comes first"); 
else 
    NSLog(@"date1 is the same date as date2"); 
+0

당신은 correct.now입니다. 이것은 다음과 같이 보이고 있습니다. first = 2012-12-28 00:00:00 +0000 – suji

+0

만약 내가이 형식으로 28/12/2012에 그것을 설정하고 싶다면 . 제발 도와주세요. – suji

+0

NSDate의 '설명'형식입니다. 다시 NSString으로 변환하려면 동일한 포맷터와'[dtFormatter stringFromDate : date]'를 사용할 수 있습니다. – John

1
 dtFormatter = [[NSDateFormatter alloc] init]; 
     [dtFormatter setDateFormat:@"dd/MM/yyyy"]; 
     NSDate *strDate = [[NSDate alloc] init]; 
     strDate = [dtFormatter dateFromString:fromDates]; 
     NSLog(@"First = %@",strDate); 

내가 당신에게 도움이 될 것이라 생각합니다.

관련 문제