gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:34:42 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com