活动倒计时关于时间的处理

<code>

  • (void)timeWithStr:(NSString )str{
    <code>
    NSDate
    dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]1000;
    NSInteger b = (NSInteger)a;//当前系统时间戳
    NSInteger x = [str integerValue];
    NSInteger d = (x -b);
    CGFloat tep = 24
    60601000;//tep 表示一天
    if (x > tep) { //显示天数
    NSInteger day = floor((d/tep));//向下取整
    [self.surplusButton setTitle:[NSString stringWithFormat:@"剩余:%ld天",day] forState:UIControlStateNormal];
    }else{//进入倒计时
    //进入倒计时后将(毫秒数差)转成小时取整,分取整,秒取整
    self.timeSec = d/1000;
    [self startTimer];
    }
    </code>
    }
  • (void)startTimer

{
<code>
if (timer) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshLessTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
</code>
}

  • (void)refreshLessTime{
    <code>
    [self.surplusButton setTitle:[NSString stringWithFormat:@"剩余:%02ld:%02ld:%02ld",self.timeSec/3600,self.timeSec%3600/60,self.timeSec%60] forState:UIControlStateNormal];
    </code>
    self.timeSec--;
    if (self.timeSec==0) {
    [self.surplusButton setTitle:@"已结束" forState:UIControlStateNormal];
    self.surplusButton.backgroundColor = [UIColor lightGrayColor];
    [timer invalidate];
    timer = nil;
    }
    }
    </code>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容