2013-06-05 96 views
0

IAM比較兩個日期,但是當我的兩個日期輸入到一個字符串變量它的工作,但是當IAM通過這些valuse到的數據進行比較梅索德它不工作,爲什麼?NSDate比較顯示錯誤的值?

這是使用代碼IAM?

NSString *dateString [email protected]"2013-05-12 22:10:02"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
// this is imporant - we set our input date format to match our input string 
// if format doesn't match you'll get nil from your string, so be careful 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"]; 
NSDate *dateFromString = [[NSDate alloc] init]; 
// voila! 
dateFromString = [dateFormatter dateFromString:dateString]; 



NSString *dateString1 = @"2013-03-04 07:59:08"; 
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
// this is imporant - we set our input date format to match our input string 
// if format doesn't match you'll get nil from your string, so be careful 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:MM:SS"]; 
NSDate *dateFromString1 = [[NSDate alloc] init]; 
// voila! 
dateFromString1 = [dateFormatter dateFromString:dateString1]; 

if ([dateFromString compare:dateFromString1] == NSOrderedDescending) 
{ 
    NSLog(@"1 is later than 2"); 

} else if ([dateFromString compare:dateFromString1] == NSOrderedAscending) 
{ 
    NSLog(@"1 is earlier than 2"); 

} else 
{ 
    NSLog(@"dates are the same"); 

} 

**當IAM傳遞的日期(格式的NSString)向梅索德

(int)compareDate:(NSString *)date1:(NSString *)date2 

在該方法我寫上面的比較代碼 所以IAM主叫此梅索德像

[self compareDate:date1:date2] 

和IAM總是得到「日期是相同的」值?當我手動鍵入日期themn IAM得到確切的答案?這是爲什麼?**

+1

大概是因爲它們都爲null。檢查你的格式字符串。注意你在分鐘和幾個月都使用MM? '____-'和SS而不是ss幾秒? '-______________________-' – borrrden

+0

好吧,我會檢查 – Navi

回答

2

通過下面的代碼替換代碼。 「mm」格式爲分鐘,「MM」格式爲幾個月。並使用「SS」秒而不是「SS」。閱讀蘋果文檔,瞭解有關日期格式化程序的詳細信息。

NSString *dateString [email protected]"2013-05-12 22:10:02"; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    // this is imporant - we set our input date format to match our input string 
    // if format doesn't match you'll get nil from your string, so be careful 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSDate *dateFromString = [[NSDate alloc] init]; 
    // voila! 
    dateFromString = [dateFormatter dateFromString:dateString]; 



    NSString *dateString1 = @"2013-03-04 07:59:08"; 
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
    // this is imporant - we set our input date format to match our input string 
    // if format doesn't match you'll get nil from your string, so be careful 
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSDate *dateFromString1 = [[NSDate alloc] init]; 
    // voila! 
    dateFromString1 = [dateFormatter dateFromString:dateString1]; 

    if ([dateFromString compare:dateFromString1] == NSOrderedDescending) 
    { 
     NSLog(@"1 is later than 2"); 

    } else if ([dateFromString compare:dateFromString1] == NSOrderedAscending) 
    { 
     NSLog(@"1 is earlier than 2"); 

    } else 
    { 
     NSLog(@"dates are the same"); 

    } 
+0

好的謝謝你的修改答案....बहुतबहुतधन्यवाद – Navi

1

問題是與日期formatter.Make使用mm代替MM幾分鐘和ss而不是SS爲秒。

[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
+0

mm而不是MM **分鐘** -1 – borrrden

+0

@borrrden對不起,這是一個錯字。感謝 –

+1

它發生,刪除我的downvote,因爲你修復它:) – borrrden