user1482450
user1482450

Reputation:

Compare two NSStrings representing dates

I am using NSSortDescriptor for comparing two dates; it works well. But I want to compare two date strings like "oct-22,2012 Morning" and "oct-22,2012 Evening".

I have separated each string with a space and compared the dates without the morning/evening strings, but I want to display the entire format above i.e., upcoming status is "oct-22,2012 Morning".

My code for comparing two dates is below:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"convertedDate" ascending:YES] ;
[sortArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];   
GetBuddiesObject *datewiseSortObj;                                   

if([sortArray count]>0)
{
     datewiseSortObj=[sortArray objectAtIndex:0];
     obj.flying=datewiseSortObj.flying;                  
     obj.when=datewiseSortObj.when;
     obj.herenow=datewiseSortObj.herenow;                  
     obj.convertedDate=datewiseSortObj.convertedDate;
}

Is there any way to do this?

Upvotes: 0

Views: 367

Answers (1)

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

The safest approach is to use a date formatter to convert the strings to NSDate object then compare those using standard techniques.

Upvotes: 2

Related Questions