2011-12-30 269 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你會得到「奇怪」的結果。