iphone - 获取 iPhone 中当前的小时、分钟和秒

标签 iphone datetime time timezone timestamp

如何从中获取当前的小时、分钟和秒?

NSDate *now = [NSDate date];

谢谢!我想要 3 个 int 变量,其中存储有值,而不是显示值的字符串。

最佳答案

您需要使用NSDateComponents ,这有点棘手(更不用说冗长了!),因为您需要了解一点 Cocoa 处理日历的方式。 docs中有一个很棒的介绍。 ;这是修改后的摘录:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components =
                    [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];

关于iphone - 获取 iPhone 中当前的小时、分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361087/

相关文章:

PHP 操纵时区?

iphone - 将文档同步到 iCloud

c# - 将 EST 转换为 IST 在 C# 中给出错误

mysql - 对日期/时间事件的 "Blocks"组进行 SQL 查询?

python - Django 从 models.DateTimeField 获取月份

python - 如何使用 python 或 pandas 根据由字典列表组成的列来过滤 DataFrame?

html - 为 iOS 居中 HTML 输入字段

iphone - AVMutableVideoComposition 中的淡入淡出

iphone - 从 tableviewcontroller 推送时的黑色背景

iphone - iOS MVC 应用程序的关注点分离