iphone - 比较日期

标签 iphone ios ios5

我正在尝试比较日期,但不是比较 2011 年 12 月 31 日与 2012 年 1 月 1 日这样的日期。我试图删除 2011 年 12 月 31 日的值和 2012 年 1 月 1 日之前的任何其他值。我在这里这样做:我想显示今天和即将到来的日期的值。

-(void)showcurrentdate
{
    NSDate *objdate=[NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"dd-MM-YYYY"];

NSDate *str1= (NSDate *)[dateformatter stringFromDate:objdate];

NSLog(@"configuregameArray......%d",[configuregameArray count]);

if([configuregameArray count]>=1)
{    
  NSMutableArray* indexarray= [[NSMutableArray alloc]init];  
  for(int k =0; k<[configuregameArray count]; k++)
 {

    NSDate *datefromarray = (NSDate *)[[configuregameArray objectAtIndex:k] objectForKey:@"gd"];  

     NSComparisonResult result = [datefromarray compare:str1];

     switch (result)
     {               

         case NSOrderedAscending:

             NSLog(@"%@ is greater from %@", str1 ,datefromarray);
             indexNumber = [NSString stringWithFormat:@"%d",k];
             [indexarray addObject:indexNumber];
             NSLog(@"indexarray......%@",indexarray);
             break;


         default: 
             NSLog(@"going fine");   
                            break;
     }

 }


    NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
    for(int i=0; i<[indexarray count]; i++)
    {
       NSInteger removeindex = [[indexarray objectAtIndex:i]intValue];        
       [discardedItems addIndex:removeindex];

     }

    [configuregameArray removeObjectsAtIndexes:discardedItems];
}
NSLog(@"configuregameArray......%@",configuregameArray);

}

最佳答案

假设您将日期作为字符串存储在另一个数组中,并且您希望以字符串数组结尾,则仅过滤掉特定日期之后发生的日期将采取以下步骤:

  • 获取要过滤的日期
  • 遍历所有字符串
    • 使用日期格式化程序将字符串转换为日期对象。
    • 将转换后的日期与过滤后的日期进行比较
      • 如果转换后的日期在过滤日期之后,则存储日期字符串的索引
  • 使用您在循环所有字符串时存储的索引创建一个新的字符串数组。

显然,将日期存储在数组中会更好。这使所有日期计算变得更加容易。然后你可以用一行 NSPredicate 而不是多行代码来过滤数组。将对象存储为字符串通常是一个糟糕的设计。更好的做法是将对象存储为对象,并在将它们呈现在用户界面中时将它们转换为字符串。这是您可以更改对象的格式化方式,而无需更改它们的存储、转换和使用方式。这是一种关注点分离,其中关注将日期显示为字符串的接口(interface)将日期转换为字符串,而关注日期的模型使用日期并且根本不关心字符串。

无论如何,用于过滤问题中日期字符串 的上述步骤的代码如下所示。如果您想使用今天的日期进行过滤,您可以使用 [NSDate date]; 将今天的日期作为日期而不是字符串。

    // Create some sample date strings 
    NSArray *dateStrings = [NSArray arrayWithObjects:@"01-01-2012", @"03-01-2012", @"01-03-2012", @"10-08-2011", @"06-02-2002", @"20-04-1999", @"01-01-2012", @"31-12-2011", @"07-03-2014", @"18-09-3456", @"27-07-1924", nil];

    // The same kind date format as the question
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"dd-MM-YYYY";

    // The date that other dates are filtered using
    NSDate *filterDate = [dateFormatter dateFromString:@"01-01-2012"];

    ///////////// OUTPUT /////////////
    NSLog(@"BEFORE: %@", dateStrings);
    //////////////////////////////////

    // Get the indexes of all dates after the filterDate.
    NSIndexSet *newerDateIndexes = [dateStrings indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        NSString *dateString = (NSString *)obj; // The string stored in the array
        NSDate *date = [dateFormatter dateFromString:dateString]; // Use the dateFormatter to get a date from the array

        // Add this index to the index set if the date occurs after the filterDate.
        return ([date compare:filterDate] != NSOrderedAscending);
    }];

    // A new array with only the dates after the filterDate
    NSArray *filteredDateString = [dateStrings objectsAtIndexes:newerDateIndexes];

    ///////////// OUTPUT ///////////////////
    NSLog(@"AFTER: %@", filteredDateString);
    ////////////////////////////////////////

关于iphone - 比较日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677832/

相关文章:

ios - 为什么在 iOS 应用程序上整个屏幕放大?

ios - 以编程方式停止向 iPhone 发送短信

ios - xcodebuild 成功时退出状态为 1

iphone - 检查文档目录中是否存在文件

python - 我无法使用 PythonKit 在 Xcode 11 中导入 Python 模块

ios - React Native Swipeable(滑动删除)未关闭

ios - iOS 5模拟器

iphone - MKMapView 警报自定义

ios - 更新的 ios5 应用程序 xcode 不会在 __dyld__dyld_start 运行停止

iphone - Iphone 7 和 7 plus 的 CSS 媒体查询