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得到確切的答案?這是爲什麼?**
大概是因爲它們都爲null。檢查你的格式字符串。注意你在分鐘和幾個月都使用MM? '____-'和SS而不是ss幾秒? '-______________________-' – borrrden
好吧,我會檢查 – Navi