作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个计时器,根据从服务器获取的字符串进行倒计时。该字符串输出剩余的总秒数。
我正在使用这段代码 - 它正确计算初始数字时间并倒计时,但分钟是问题所在。
编辑 - 每实际一分钟,分钟计算就会增加一。但我不明白为什么要这样做。
-(void)showTimmer:(id) 发件人{
//get total amount of seconds
NSString *timeRaw = [ArrayFromServer objectAtIndex:1];
NSArray *timeArr = [timeRaw componentsSeparatedByString:@","];
timetoInt = [timeArr objectAtIndex:0];
int time1 = [timetoInt intValue];
//set the second countdown
NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSSecondCalendarUnit) fromDate:now];
NSInteger seconds = time1 - [dateComponents second];
[gregorian release];
// do the hours days maths
int hour = (seconds / 3600 );
seconds -= hour * 3600;
int minute = (seconds / 60 );
seconds -= minute * 60;
int second = seconds;
//set the labels
countdownLabel.text = [NSString stringWithFormat:@"%02dH %02dM %02ds", hour, minute, second];
最佳答案
在进入下一行之前,您需要从总秒数中减去计算出的小时数(不知道为什么使用模数)。像这样的东西(注意
int hour = (seconds / 3600 );
seconds -= hour * 3600;
int minute = (seconds / 60 );
seconds -= minute * 60;
int second = seconds;
希望能解决这个问题。
关于iphone - 时间计算给出越来越错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439282/
我是一名优秀的程序员,十分优秀!