gpt4 book ai didi

iphone - NSDate 为 "beginning of next hour"

转载 作者:行者123 更新时间:2023-12-03 19:02:37 25 4
gpt4 key购买 nike

我想对下一个整小时进行倒计时。倒计时到特定时间非常容易,例如:

NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"midnight tomorrow"]; 

如何定义“每个小时的开始”的 NSDate?

谢谢!

编辑:这就是我目前所拥有的。无法将解决方案集成到我的代码中。任何帮助将不胜感激。 :)

-(void)updateLabel {
NSDate *now = [NSDate date];

NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"midnight tomorrow"];

//num of seconds between mid and now
NSTimeInterval timeInt = [midnight timeIntervalSinceDate:now];
int hour = (int) timeInt/3600;
int min = ((int) timeInt % 3600) / 60;
int sec = (int) timeInt % 60;
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
}

最佳答案

由于 +dateWithNaturalLanguageString 仅在 MacOS SDK 上可用,并且您的目标是 iPhone,因此您需要创建自己的方法。我认为 NSCalendar 类可以帮助你:

- (NSDate*) nextHourDate:(NSDate*)inDate{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components: NSEraCalendarUnit|NSYearCalendarUnit| NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit fromDate: inDate];
[comps setHour: [comps hour]+1]; // Here you may also need to check if it's the last hour of the day
return [calendar dateFromComponents:comps];
}

我还没有检查过这段代码,但它(至少是这种方法)必须有效。

关于iphone - NSDate 为 "beginning of next hour",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2309337/

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