2013-10-29 192 views
-2

如何開始日期和結束日期在iPhone SDK的代碼比較?如何檢查日期是否在兩個日期之間?

示例: - >

if ([nowdate compare:raceStartDate] == NSOrderedDescending && [nowdate compare:raceEndDate] == NSOrderedAscending) 
{ 

} 
+0

http://stackoverflow.com/questions/15033769/checking-if-date-is-between-範圍,http://stackoverflow.com/questions/6250383/date-in-a-specific-range-with-nsorderedascending-and-nsordereddescending – user2864740

回答

2
NSDate *date1; 
NSDate *date2; 


if ([date1 compare:date2] == NSOrderedDescending) { 
    NSLog(@"date1 is later than date2");   

} else if ([date1 compare:date2] == NSOrderedAscending) { 
    NSLog(@"date1 is earlier than date2"); 

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

} 
0

嘗試此

- (BOOL)date:(NSDate*)nowdate isBetweenDate:(NSDate*)raceStartDate andDate:(NSDate*)raceEndDate 
{ 
    if ([nowdate compare:raceStartDate] == NSOrderedAscending) 
     return NO; 

    if ([nowdate compare:raceEndDate] == NSOrderedDescending) 
     return NO; 

    return YES; 
} 
相關問題