2012-04-19 4 views
0

현재 날짜와 60 일 사이의 이벤트 날짜를 확인해야합니다. 아래 코드가 사용되었지만 제대로 작동하지 않습니다. 내 서버의 "2012-04-14T16 : 50 : 02Z"과 같은 이벤트 문자열이 표시됩니다.iPhone : 현재 날짜와 현재 날짜의 60 일 간의 날짜 비교 문제

// current date 
double currDateInMilliSecs = [NSDate timeIntervalSinceReferenceDate] * 1000;  
NSLog(@"currDateInMilliSecs: %f", currDateInMilliSecs); 

// sixty days 
double sixtydaysvalue = 60.0 * 24.0 * 3600.0 * 1000.0; 
NSLog(@"sixtydaysvalue: %f", sixtydaysvalue); 
// add current date + sixt days 
double sixtyDaysMilliSecsFromCurrDate = currDateInMilliSecs + sixtydaysvalue; 
NSLog(@"sixtyDaysMilliSecsFromCurrDate: %f", sixtyDaysMilliSecsFromCurrDate); 

// check does the event date between current date + 60 days 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
//[df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
//[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

// [eventDict objectForKey:@"begin_at"] gives date string like this "2012-04-14T16:50:02Z" for ex. 
NSDate *eventdate = [df dateFromString:[eventDict objectForKey:@"begin_at"]]; 

NSTimeInterval nowSinceEventDate = [eventdate timeIntervalSince1970]; 
NSLog(@"nowSinceEventDate: %f", nowSinceEventDate); 
double eventDateInMilliSecs = nowSinceEventDate * 1000; 
NSLog(@"eventDateInMilliSecs: %f", eventDateInMilliSecs); 

// this is not working as expected  
if (eventDateInMilliSecs<sixtyDaysMilliSecsFromCurrDate && eventDateInMilliSecs>currDateInMilliSecs) 
{ 


} 
else 
{ 
} 
+0

어떤 방식으로 작동하지 :

문제에 대한 간단한 해결책 (여기에 우리가 eventdate는 당신이 당신의 포맷을 사용하여 만든 날짜가된다고 가정)? – jtbandes

+0

NSDate에 대해 compare 함수를 사용합니다. – priyanka

+0

Milli 초가 이와 같이 예기치 때문에 현재 DateInMilliSecs : 356532141059.288025가 예상대로 비교되지 않습니다. SixtyDaysMilliSecsFromCurrDate : 361716141059.288025; eventDateInMilliSecs : 1336172400000.000000은 실제로 2012-05-04 19:00:00 +0000입니다. 그래서, 실제로 60 일부터 오늘 날짜 아래에오고 있지만, 내 상태가 예상대로 작동하지 않습니다. – Getsy

답변

2

희망 내 프로젝트에 대한 최신의 작업을 비교하는 제 기능을 사용 . 대신, Cocoa Touch 프레임 워크의 것들을 사용해보십시오. 계산이 다소 단순하기 때문에 날짜를 사용하여 계산할 수 있지만 NSCalendar와 NSDateComponents를 사용하면 훨씬 복잡한 작업을 쉽게 처리 할 수 ​​있습니다.

NSDate* now = [NSDate date]; 
NSDate* sixtyDaysFromNow = [now dateByAddingTimeInterval:(60 * 60 * 24 * 60)]; 
if([eventDate earlierDate:now] == now && [eventDate laterDate:sixtyDaysFromNow] == sixtyDaysFromNow) { 
    // the event date is within scope 
} else { 
    // the event date is outside stop 
} 
+0

죄송합니다.이 정보는 올바르게 업데이트되지 않습니다. – Getsy

+0

@Getsy "업데이트"란 무엇을 의미합니까? –

+0

죄송합니다. 내가 잘못했습니다. – Getsy

0

내가 그 도움 당신에게 그것이 아니다 자바, 것처럼 당신은이 문제를 해결하기 위해 노력하고

-(BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endingDate 

{ 

    if ([date compare:beginDate] == NSOrderedAscending) 
     return NO; 

    if ([date compare:endingDate] == NSOrderedDescending) 
     return NO; 

    return YES; 
} 
+0

죄송합니다, 이것은 올바르게 업데이트되지 않습니다. – Getsy

+1

미안해. 내가 틀렸어. – Getsy

+0

괜찮습니다. 도움이 되었기를 바랍니다. –

관련 문제