<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 = 2460601000;//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>