2014-03-27 68 views
1

我有日期的數組格式爲:如何將日期對象與Objective-C中的字符串進行比較?

date: 2012-01-02,2012-03-17,2012-04-09,2012-05-07,2012-06-04,2012-08-06,2012-10-29,2012-12-25,2012-12-26. 

我想比較今天的日期的日期,但我需要一些幫助。這是我的代碼。

NSArray *date =[dict12 objectForKey:@"ie_date_closed"]; 
NSLog(@"date:%@",date); 
int i=date; 
for (i=0; i<6; i++) 
{ 
    NSComparisonResult result = [todaydate compare:date[i]]; 
    NSLog(@"result:%d",result); 
} 
+0

您面臨的問題是什麼? – Mayur

+0

我只是嘗試這是行不通的。幫助我使用你的代碼。 – janagowtham

+0

我想比較nsarray日期和今天的日期。 – janagowtham

回答

2

試試下面的代碼: -

NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; 
[formatter setDateFormat:@"yyyy-MM-dd"]; 
for (NSString *arrDt in date){ 
if ([arrDt isEqualToString:[formatter stringFromDate:[NSDate date]]]){ 
NSLog(@"Equal Date");}} 

注: - 你需要根據數組對象日期設定日期格式。

2

下面是代碼看看&嘗試使用它來實現你的自我:

for (i=0; i<[date count]; i++) 
{ 
    NSDateformatter *format = Define Your date Format Here 
    NSDate yourDate = [format dateFromString:[date objectAtIndex:i]]; 
    If ([YourDate compare:[NSDate date]] == NSOrderedSame) 
    { 
     NSLog(@"Both Dates are same"); 
    } 
} 

注:我不會給你整個現成的代碼,但只是一個提示,以便使用它,你可以實現你想要的。

希望這會有所幫助。

2

嘗試

NSDate *firstTime; 
    NSDate *nowTime; 
    NSDate *localDate = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    dateFormatter.dateFormat = @"YYYY-MM-dd"; 
    NSString *str = [dateFormatter stringFromDate:localDate]; 


     NSArray *date =[dict12 objectForKey:@"ie_date_closed"]; 
     NSLog(@"date:%@",date); 
    for (i=0; i<[date count]; i++) 
{ 


    NSString *openTime =[NSString stringWithFormate:"%@"[date objectAtindex:i]]; 
    NSDateFormatter *dateComperFormatter1 = [[NSDateFormatter alloc] init]; 
    [dateComperFormatter1 setTimeZone:[NSTimeZone systemTimeZone]]; 
    [dateComperFormatter1 setDateFormat:@"YYYY-MM-dd"]; 


    firstTime = [dateComperFormatter1 dateFromString:openTime]; 
    nowTime = [dateComperFormatter1 dateFromString:str]; 
    // NSComparisonResult result; 
    // // has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending 
    // 
    // result = [nowTime compare:firstTime]; // comparing two dates 

    NSComparisonResult result = [nowTime compare:firstTime]; 
    } 
相關問題