iOS. 时间段内倒计时

 //服务器时间
 NSString *serviceString = [NSDate getTimeStrByTimeSp:serverTime format:@"YYYY-MM-dd HH:mm:ss"];
 NSLocale *CurrenLocale = [NSLocale currentLocale];
 NSTimeZone *localZone = [NSTimeZone timeZoneWithName:@"UTC"];
 NSDate *serverDate = [NSDate dateWithString:serviceString format:@"YYYY-MM-dd HH:mm:ss" timeZone:localZone locale:CurrenLocale];

 //开始时间
 NSDate *seckillDate = [NSDate dateWithString:exchangeListInfo.seckillStartTime format:@"YYYY-MM-dd HH:mm:ss" timeZone:localZone locale:CurrenLocale];
  
seckillDate 是NSDate类型   serverDate 是 NSDate 类型
 NSTimeInterval futureTimeInterval = [seckillDate timeIntervalSinceDate:serverDate];

根据时间段开始计时
- (void)countdownTime
{
    if (!self.isStart) {
        NSInteger interval = self.interval - 1;
        if (interval <= 0) {
            self.isStart = YES;
        }else {
            if (interval <= 180) {
                self.willStart = YES;
            }
            self.interval = interval;
        }
    }
}

观察者方法 观察isStart willStart  interval 属性,改变文字
- (NSString *)countdownTimeText
{
    if (!self.isStart) {
        NSInteger day = self.interval/(3600*24);
        NSInteger hour = (self.interval - day*(3600*24))/3600.0;
        NSInteger minite = (self.interval - day*(3600*24) - hour*3600)/60;
        NSInteger second = (self.interval - day*(3600*24) - hour*3600 - minite*60);

        //到这里已经可以获取到具体的时差 这边可以显示在你
        if (day > 0) {
            NSString *timeStr = [NSString stringWithFormat:@"距离开抢还剩 %ld天 %02ld:%02ld:%02ld",day,hour,minite,second];
            return timeStr;
        }else {
            NSString *timeStr = [NSString stringWithFormat:@"距离开抢还剩 %02ld:%02ld:%02ld",hour,minite,second];
            return timeStr;
        }
    }
    return nil;
}

你可能感兴趣的:(iOS. 时间段内倒计时)