【代碼筆記】兩個時間字串的比較,筆記字串
一,。
二,代碼。
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. [self initTimerCompare]; }#pragma -mark -functions//比較時間-(void)initTimerCompare{ NSString *starTimer=@"2014-08-29"; NSString *finishTimer=@"2014-09-30"; BOOL result = [starTimer compare:finishTimer] == NSOrderedSame; NSLog(@"result:%d",result); if (result==1) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"開始時間和結束時間相等" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]; [alert show]; return; } BOOL result1 = [starTimer compare:finishTimer]==NSOrderedDescending; NSLog(@"result1:%d",result1); if (result1==1) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"開始時間晚於結束時間" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]; [alert show]; return; } BOOL result2 = [starTimer compare:finishTimer]==NSOrderedAscending; NSLog(@"result2:%d",result1); if (result2==1) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"開始時間早於結束時間" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]; [alert show]; return; }}