iOS实现倒计时label显示

NSDate *now = [NSDate date];
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
    [dateFormater setDateFormat:@"yyyyMMddHHmmss"];
    NSString *nowtime = [NSString stringWithFormat:@"%@",[dateFormater stringFromDate:now]];

  _timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daoJi:) userInfo:_keyTime repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    _daojishi = [nowtime integerValue]-[_keyTime integerValue];

- (NSString*)getTime{
    if (!_keyTime) {
        return @"暂无活动";
    }
    id obj = _keyTime;
    NSString *stayears = [obj substringToIndex:4];
    NSString *stanowmouths = [obj substringFromIndex:4 toIndex:6];
    NSString *stanowday = [obj substringFromIndex:6 toIndex:8];
    NSString *stanowhour = [obj substringFromIndex:8 toIndex:10];
    NSString *stanowminute = [obj substringFromIndex:10 toIndex:12];
    NSString *stanowsecond = [obj substringFromIndex:12 toIndex:14];
    
    NSDateFormatter *f1 = [[NSDateFormatter alloc] init];
    [f1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *theDay = [f1 dateFromString:[NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@",stayears,stanowmouths,stanowday,stanowhour,stanowminute,stanowsecond]];
    
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象
    
    NSDate *today = [NSDate date];//得到当前时间
    
    //用来得到具体的时差
    unsigned int unitFlags =  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:theDay options:0];
    

    if ([d minute]<10 && [d hour]<10 &&[d minute] >0 && [d hour]>0) {

        NSString *countdown = [NSString stringWithFormat:@"0%ld:0%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];

        return countdown;

    }

    else if([d hour]<10 && [d minute]>10){

        NSString *countdown = [NSString stringWithFormat:@"0%ld:%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];

        return countdown;

        

    }

    else if([d minute]<10 && [d hour]>10){

        NSString *countdown = [NSString stringWithFormat:@"%ld:0%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];

        return countdown;

    }

    else if([d hour] <=0 && [d minute] <= 0 && [d second] <= 0) {

        [_timer invalidate];

        return @"本次活动已经开始,赶快抢购吧~";

    }

    else{

        NSString *countdown = [NSString stringWithFormat:@"%ld:%ld:%ld", (long)[d hour], (long)[d minute], (long)[d second]];

        return countdown;

    }


}
- (void)daoJi:(NSTimer*)aTimer
{
    _remainLa.text = [self getTime];
   

    return ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值