爱程序网

【代码笔记】两个时间字符串的比较

来源: 阅读:

一,效果图。

二,代码。

复制代码
- (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;
    }

}
复制代码

 

 

 
 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助