我有一个跨越多年的时间间隔,我希望所有的时间组件从一年到几秒.
我的第一个想法是将一年中的时间间隔除以整数秒,从运行的总秒数中减去该值,将其除以一个月中的秒数,从运行总计中减去该值,依此类推.
这看起来很复杂,我已经读过,无论何时你做一些看起来错综复杂的事情,都可能有内置的方法.
在那儿?
我将Alex的第二种方法整合到我的代码中.
它位于我的界面中由UIDatePicker调用的方法中.
NSDate *now = [NSDate date];
NSDate *then = self.datePicker.date;
NSTimeInterval howLong = [now timeIntervalSinceDate:then];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:howLong];
NSString *dateStr = [date description];
const char *dateStrPtr = [dateStr UTF8String];
int year, month, day, hour, minute, sec;
sscanf(dateStrPtr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &sec);
year -= 1970;
NSLog(@"%d years\n%d months\n%d days\n%d hours\n%d minutes\n%d seconds", year, month, day, hour, minute, sec);
Run Code Online (Sandbox Code Playgroud)
当我将日期选择器设置为过去1年和1天的日期时,我得到:
1年1个月1天16小时0分20秒
这是1个月和16个小时.如果我将日期选择器设置为过去的1天,我的数量相同.
更新:我有一个应用程序,计算你的年龄,给定你的生日(从UIDatePicker设置),但它经常关闭.这证明存在不准确性,但我无法弄清楚它来自哪里,可以吗?
一直在搜索ios中的mod运算符,就像%在c中一样,但没有找到它的运气.尝试在此链接中的答案,但它给出了相同的错误.我有一个浮点变量'rotationAngle',其角度根据用户的手指移动保持递增或递减.有点像这样:
if (startPoint.x < pt.x) {
if (pt.y<936/2)
rotationAngle += pt.x - startPoint.x;
else
rotationAngle += startPoint.x - pt.x;
}
rotationAngle = (rotationAngle % 360);
}
Run Code Online (Sandbox Code Playgroud)
我只需要确保rotationAngle不超过+/- 360限制.任何帮助任何身体.谢谢