常德赵炯吧 关注:2贴子:16
  • 0回复贴,共1

获取当前时间戳的方法

取消只看楼主收藏回复

-(void)showRemainingTimeWithOeverTime:(NSInteger)overTime
{
//获取当前时间的时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
NSInteger nowTime=[timeString integerValue];
//计算剩余时间并且显示在label上
NSInteger remainingTime=overTime-nowTime;
self.text=[NSString stringWithFormat:@"%ld:%ld:%ld",remainingTime%(60*60*60)/(60*60),remainingTime%(60*60)/60,remainingTime%60];
NSString *remainingTimeStr=[NSString stringWithFormat:@"%ld",remainingTime-1];
//将一秒之后的时间传入函数并延迟一秒执行
[self performSelector:@selector(changeTime:) withObject:remainingTimeStr afterDelay:1.0f];
}
-(void)changeTime:(NSString *)remainingTimeStr
{
//改变lable的文字
NSInteger remainingTime=[remainingTimeStr integerValue];
self.text=[NSString stringWithFormat:@"%ld:%ld:%ld",remainingTime%(60*60*60)/(60*60),remainingTime%(60*60)/60,remainingTime%60];
//当前时间减去一秒并延迟一秒执行此函数造成递归效果
NSString * remainingTimeStr2=[NSString stringWithFormat:@"%ld",remainingTime-1];
[self performSelector:@selector(changeTime:) withObject:remainingTimeStr2 afterDelay:1.0f];
NSLog(@"1");
}


IP属地:湖南1楼2015-12-04 13:08回复