2011-12-30 193 views
1

我试图比较日期,但它不能与2012年1月1日比较像31日 - 12月2011日。我试图在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); 

} 
+0

您是否有日期数组,并且只想过滤出某个日期之后的日期,例如: 2012年1月1日?你可以使用-indexesOfObjectsPassingTest过滤这些:(如果我正确理解问题,我会写一个答案) – 2011-12-30 09:20:51

+0

@DavidRönnqvist其实我有一个数组(configuregamearay),我把日期作为字符串存储在键值'gd'中。我在'datefromarray'中获取相同的内容并与当前日期进行比较。我想从具有日期早于当前日期的configuregamearay中删除值。 – Ketan 2011-12-30 09:39:24

+0

那么,'[[configuregameArray objectAtIndex:k] objectForKey:@“gd”]'实际上给你一个'NSDate'还是一个'NSString'? – DarkDust 2011-12-30 09:44:41

回答

1

既然你有你的日期存储在另一个字符串数组,并要使用字符串数组来结束,筛选出只在特定日期后发生的日期将采取以下步骤:

  • 获取日期在所有字符串
    • 环路滤波器使用日期格式的字符串转换为日期对象。
    • 比较转换后的日期过滤日期
      • 店日期字符串的索引,如果过滤器日期之后发生转换的日期
  • 创建的字符串与指标的新数组你在循环所有字符串时存储。

显然,这将是更好的存储阵列中的日期。这使得所有的日期计算变得更容易。然后你可以用一行NSPredicate过滤数组,而不是多行代码。将对象存储为字符串通常是不好的设计。更好的做法是将对象存储为对象,并在将它们呈现在用户界面中时将其转换为字符串。这是你可以改变对象的格式,而不必改变它们的存储,转换和使用方式。这是一个关注点分离的问题,即关注将日期显示为字符串的界面会将日期转换为字符串,而关注日期的模型使用日期而根本不关心字符串。

无论如何,上述步骤的代码过滤日期字符串在你的问题看起来像下面。如果你想利用今天的日期筛选,您可以使用[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); 
    //////////////////////////////////////// 
0

您不能简单地将一个类转换为另一个类,除非它们相关(转换为超类/子类)。

所以,你应该做这样的:

  • 无论是商店阵列中NSDate实例。然后,您只需将日期与您的objdate进行比较即可。那将是最好的解决方案。
  • 还是把每一个字符串转换成日期(使用[dateformatter dateFromString:]),并与您的objdate比较日期。
  • 您应该将字符串表示不能比拟的,如果你没有我们反向格式像YYYY-MM-dd你会得到“奇怪”的结果。