2013-07-26 2 views
-2

나는 currentdate와 getDate와 같은 두 개의 날짜를 가지고 있는데, 나는 getdate가 old 또는 new 또는 equal과 비교하기를 원한다. objectiveC에서 두 날짜 비교?

내가 참조를 위해이 시도 :

NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MM/dd/yyyy"]; 
    NSString *currentDate = [dateFormatter stringFromDate:today]; 
    // Current_date (7/26/2013) 

    NSString *getdate=[outDateformatter1 stringFromDate:bDt1]; 
    // Get_date (7/26/2010) 

    NSComparisonResult result;   
    result = [currentDate compare:getdate]; // comparing two dates 
    if(result==NSOrderedAscending) 
     NSLog(@"today is less"); 

    I need this, Original result getdate is old date. 

그러나이 제대로 작동하지 않는, 어떤 일이 저를 제안 해주십시오. 사전

+0

]을 참조 링크 http://stackoverflow.com/questions/7901061/comparing-two-dates-with-nsdate?rq=1 – Pradeep

+1

당신이 날짜를 비교할 때,있는 NSDate 당신이 그들로부터 생성 문자열이 아닌 객체 비교 , isEqualToDate :를 사용하십시오. – rdelmar

+0

날짜 객체를 문자열이 아닌 비교합니다. –

답변

0
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
    NSDate *serDate; 

    NSDate *endDate; 

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

    serDate = [formatter dateFromString:@"2012-12-07 7:17:58"]; 

    endDate = [formatter dateFromString:@"2012-12-07 08:43:30"]; 
    NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:serDate]; 

    double minutes = timeDifference/60; 

    double hours = minutes/60; 

    double seconds = timeDifference; 

    double days = minutes/1440; 
    NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds); 


    if (hours > 1.30) 
     return 1; 
    else 
     return 0; 
+0

실제로 getdate는 상수 값이 아닙니다. – SampathKumar

+0

endDate = [포맷터 dateFromString : getdate]; – Rajneesh071

0

사용 compare 방법

감사합니다.

if ([currentdate compare: getDate] == NSOrderedDescending) { 
    NSLog(@"currentdate is later than getDate");   

} else if ([currentdate compare:getDate] == NSOrderedAscending) { 
    NSLog(@"currentdate is earlier than getDate"); 

} else { 
    NSLog(@"dates are the same"); 

} 
0
//Use this function to compare current date to another date 

- (NSInteger)numberOfDaysUntil:(NSDate *)aDate { 
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:[NSDate date] toDate:aDate options:0]; 

    //NSLog(@"Number of days >>>%d",[components day]); 

    return [components day]; 

// then use this logic 

if ([self numberOfDaysUntil:date]==0) 
    { 
     NSLog(@"Today"); 
    } 
    else if ([self numberOfDaysUntil:date]== -1) 
    { 
     NSLog(@"Yesterday"); 
    } 
    else{ 

     // if return is -2 then date is two date before current date . 

// if return is 1 then date is one future date than current day 

    } 
0
for that you have to import NSDate-Utilities.h and .m file. 

NSDateFormatter * DF = [NSDateFormatter의 ALLOC] INIT]; [df setDateFormat : @ "MM/dd/yyyy"];

float oldDate = [[df dateFromString:[dic valueForKey:@"end_date"]] timeIntervalSinceNow]; 

if (oldDate < 0) { 
    //past date 
} 
else if (oldDate > 0){ 
    //future date 
} 
else{ 
    //same date 
}