2013-07-26 5 views
-2

У меня есть две даты, такие как currentdate и getDate, я хочу сравнить getdate старую или новую или равную.Сравните две даты в объективе C?

Я попытался это для справки:

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

Refere ссылка 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 = [formatter 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] инициализации]; [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 
}