【问题标题】:Compare two NSStrings representing dates比较两个代表日期的 NSStrings
【发布时间】:2023-03-29 05:31:01
【问题描述】:

我使用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;
}

有什么办法吗?

【问题讨论】:

  • 你能告诉我你是如何排序/比较日期的吗?你用这种格式的'20121021'来比较吗?
  • 我正在使用这种格式 2012-10-21 04:00:00 +0000
  • 是否可以更改您的日期格式以与“20121021”进行比较,以便您可以按整数进行比较,并且要添加早上/中午/晚上的比较,您可以再添加一位数字分别是1/2/3..从最后一位排序后,你可以知道它是早上/中午/甚至......

标签: objective-c cocoa cocoa-touch nsstring nsdate


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 2013-03-20
    • 2017-04-28
    • 2011-09-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多