我正在使用NSSortDescriptor比较两个日期;它运作良好。但我想比较两个日期字符串,例如“ 2012年10月22日上午”和“ 2012年10月22日晚上”。

我用空格隔开了每个字符串,比较了没有早上/晚上的字符串的日期,但是我想在上方显示整个格式,即即将出现的状态是“ 2012年10月22日上午”。

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

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


有什么办法吗?

最佳答案

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

10-08 06:29
查看更多