0

NSSortDescriptor用来比较两个日期;它运作良好。但我想比较两个日期字符串,如“oct-22,2012 Morning”和“oct-22,2012 Evening”。

我用空格分隔每个字符串并比较没有早/晚字符串的日期,但我想显示上面的整个格式,即即将到来的状态是“oct-22,2012 Morning”。

我比较两个日期的代码如下:

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;
}

有没有办法做到这一点?

4

1 回答 1

3

最安全的方法是使用日期格式化程序将字符串转换为 NSDate 对象,然后使用标准技术进行比较。

于 2012-10-21T12:11:29.580 回答