在Obj-c中将秒转换为天,分钟和小时 [英] Convert seconds to days, minutes, and hours in Obj-c

查看:58
本文介绍了在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.

这篇关于在Obj-c中将秒转换为天,分钟和小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆