Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare current date to previous date in iphone?

I would like to compare the current date with another date, and if that is date is earlier than the current date, then I should stop the next action. How can I do this?

I have todays date in yyyy-MM-dd format. I need to check this condition

if([displaydate text]<currentdate)
{
    //stop next action 
}

Here if displaydate is less than todays date then it has to enter that condition.

like image 811
Honey Avatar asked Nov 05 '12 09:11

Honey


1 Answers

NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = [dateFormatter dateWithString:@"xxxxxx"]; // your date 

NSComparisonResult result; 
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

result = [today compare:newDate]; // comparing two dates

if(result==NSOrderedAscending)
    NSLog(@"today is less");
else if(result==NSOrderedDescending)
    NSLog(@"newDate is less");
else
    NSLog(@"Both dates are same");

got your solution from this answer How to compare two dates in Objective-C

like image 169
Nitin Gohel Avatar answered Oct 27 '22 00:10

Nitin Gohel