2011-03-25 4 views
0

오늘의 데이터베이스와 다른 날짜를 비교하고 싶습니다. 예를 들어, 2011-03-25를 2011-02-25와 비교하십시오.아이폰에서 두 날짜를 비교하는 방법?

NSDateFormatter에 NSDdate 데이터 형식이 필요하므로 데이터베이스 날짜를 형식화 할 수 있습니까?

+1

데이터베이스에서 오는 날짜 보인다. 그렇다면 항상 yyyy-MM-dd 형식으로되어 있으므로 문제없이 영원히 -isEqualToString을 사용할 수 있습니다. 하지만 당신이 NSDate 객체라면 @Jhaliya의 대답을보십시오. –

답변

3

Cocoa has couple of methods for this:

를 시작할 수 있습니다 :
- earlierDate :
- laterDate을 :
- 비교 :

당신이 사용하는 경우

는 - (NSComparisonResult)를 비교 : 더 SO post

그냥 블로그 게시물을 통해 이동 읽기, 거기

The receiver and anotherDate are exactly equal to each other, NSOrderedSame 
The receiver is later in time than anotherDate, NSOrderedDescending 
The receiver is earlier in time than anotherDate, NSOrderedAscending. 

: (있는 NSDate *) anotherDate을, 당신은 다시 이들 중 하나 얻을 많은 날짜 관련 유틸리티 함수.

http://arstechnica.com/apple/guides/2010/03/how-to-real-world-dates-with-the-iphone-sdk.ars/2

0

, 당신은 단지 문자열을 비교할 수 있습니다

if ([date1 compare:string2]==NSOrderedAscending) { 
} else if ([date1 compare:string2]== NSOrderedDescending) { 
} else { 
} 

그것은 것입니다 작품 날짜 필드는 문자열 비교처럼, 가장 중요한에서 이하 때문이다.

0

당신은있는 NSDate 비교 사용할 수 있습니다 또는 timeIntervalSinceReferenceDate은 - isEqualToDate - 그건 당신이있는 NSDate에서

1
NSDate *date1 = [NSDate date]; 
NSDate *date2 = [NSDate date]; 

if ([date1 compare: date2]==NSOrderedAscending) { 
    NSLog(@"YES"); 
} 
0

스위프트 에서 : 그들은 문자열로 저장처럼 질문의 모습에서

let date1 = NSDate() //some date 
let date2 = NSDate() //another date 

let firstDate = date1.earlierDate(date2) 
let lastDate = date1.laterDate(date2) 
관련 문제