在Obj-c中将秒转换为天,分钟和小时

问题描述:

在Objective-C中,如何将整数(代表秒)转换为天,分钟,小时?

In objective-c, how can I convert an integer (representing seconds) to days, minutes, an hours?

谢谢!

在这种情况下,您只需要划分即可.

In this case, you simply need to divide.

days = num_seconds / (60 * 60 * 24);
num_seconds -= days * (60 * 60 * 24);
hours = num_seconds / (60 * 60);
num_seconds -= hours * (60 * 60);
minutes = num_seconds / 60;

对于更复杂的日期计算,例如1983年1月19日下午3点之后的一千万秒内的天数,您可以将NSCalendar类与NSDateComponents一起使用. Apple的日期和时间编程指南会在这里为您提供帮助.

For more sophisticated date calculations, such as the number of days within the ten million seconds after 3pm on January 19th in 1983, you would use the NSCalendar class along with NSDateComponents. Apple's date and time programming guide helps you here.