<code>
- (void)timeWithStr:(NSString )str{
<code>
NSDate dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970]1000;
NSInteger b = (NSInteger)a;//當(dāng)前系統(tǒng)時(shí)間戳
NSInteger x = [str integerValue];
NSInteger d = (x -b);
CGFloat tep = 2460601000;//tep 表示一天
if (x > tep) { //顯示天數(shù)
NSInteger day = floor((d/tep));//向下取整
[self.surplusButton setTitle:[NSString stringWithFormat:@"剩余:%ld天",day] forState:UIControlStateNormal];
}else{//進(jìn)入倒計(jì)時(shí)
//進(jìn)入倒計(jì)時(shí)后將(毫秒數(shù)差)轉(zhuǎn)成小時(shí)取整,分取整,秒取整
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:@"已結(jié)束" forState:UIControlStateNormal];
self.surplusButton.backgroundColor = [UIColor lightGrayColor];
[timer invalidate];
timer = nil;
}
}
</code>