本文介绍了在Objective-C中,要获取当前小时和分钟为整数,我们需要使用NSDateComponents和NSCalendar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前的小时和分钟为整数.因此,如果现在是凌晨3:16,我想获取两个整数:3和16.

I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16.

但是看起来 [NSDate date] 将给出自1970年以来的秒数,或者它可以给出当前时间表示形式的字符串,但是没有简单的方法将它们作为整数来获取?

But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time representation, but there is no easy way to get them as integers?

我在获取当前时间中看到了一条帖子,但其中涉及 NSDateComponents NSCalendar ?那太复杂了……所需要的只是像

I see a post in Getting current time, but it involved NSDateComponents and NSCalendar? That's way too complicated... all that was need is something like

NSDate *date = [NSDate date];
int hour = [date getHour];     // which is not possible

有没有比使用3个类 NSDate NSDateComponents NSCalendar 来获取当前小时数(更简单)的方法更简单的方法,在Objective-C中,我们通常仍会使用C语言的 localtime tm 来获取小时作为整数吗?

Is there a simpler way than using 3 classes NSDate, NSDateComponents, and NSCalendar to get the current hour as an integer, or typically, in Objective-C, would we typically still use C language's localtime and tm to get the hour as an integer?

推荐答案

NSDate 仅保存自某个参考日期以来已经过的时间,以获取更多的有意义的数字除此之外(例如,在处理DST,leap年和所有其他愚蠢的时间事项之后),您必须将 NSDateComponents 与相应的 NSCalendar 一起使用.

NSDate just holds the time that has passed since a certain reference date, to get more meaningful numbers out of this (eg. after taking care of DST, leap years and all the other stupid time stuff), you have to use NSDateComponents with the appropriate NSCalendar.

这篇关于在Objective-C中,要获取当前小时和分钟为整数,我们需要使用NSDateComponents和NSCalendar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:53