我想倒计时到下一个小时。倒计时到特定时间非常容易,例如:
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];
}
答案 0 :(得分:16)
由于+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];
}
我没有检查过这段代码,但它(至少这种方法)必须有效。
答案 1 :(得分:5)
最简单的方法可能是操纵时间戳,将其四舍五入到当前小时的开头并添加1小时
- (NSDate*)nextHourDateForDate:(NSDate*)date {
NSTimeInterval timestamp = [date timeIntervalSince1970];
NSTimeInterval current = timestamp - fmod(timestamp, 3600);
NSTimeInterval next = current + 3600;
return [NSDate dateWithTimeIntervalSince1970:next];
}
答案 2 :(得分:3)
你可能想要检查一下。 Erica Sadun向NSDate发布了一些扩展。
答案 3 :(得分:2)
使用DateComponents和Date,您可以通过在指定日期添加负数分钟和一小时来获得它。
Swift3(作为扩展名):
extension Date {
public var nextHour: Date {
let calendar = Calendar.current
let minutes = calendar.component(.minute, from: self)
let components = DateComponents(hour: 1, minute: -minutes)
return calendar.date(byAdding: components, to: self) ?? self
}
}
将其与let nextHourDate = myDate.nextHour
请参阅Apple Date and Time Programming Guide以供参考。
=============================================== ============================
ObjectiveC(作为静态方法):
+ (NSDate *)nextHourDate:(NSDate *)date{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMinuteCalendarUnit|NSHourCalendarUnit fromDate:date];
components.minute = -components.minute;
components.hour = 1;
return [calendar dateByAddingComponents:components toDate:date options:0];
}
并将其命名为:
[YourClass nextHourDate:yourDate];
答案 4 :(得分:1)
我认为这样可行(注:非常简短的测试)
NSDateFormatter *currentSecondFormatter = [[NSDateFormatter alloc] init];
[currentSecondFormatter setDateFormat:@"ss"];
NSDateFormatter *currentMinuteFormatter = [[NSDateFormatter alloc] init];
[currentMinuteFormatter setDateFormat:@"mm"];
// get next hour from now
int timeUntilNextHour = ( (60 - [[currentMinuteFormatter stringFromDate:[NSDate date]] integerValue]) * 60) - [[currentSecondFormatter stringFromDate:[NSDate date]] integerValue];
答案 5 :(得分:0)
也许这会有所帮助
@implementation NSDate (DB_Extension)
- (NSTimeInterval)db_secondsForNextHour {
NSDateFormatter *minutesFormatter = [[NSDateFormatter alloc] init];
[minutesFormatter setDateFormat:@"mm"];
NSDateFormatter *secondsFormatter = [[NSDateFormatter alloc] init];
[secondsFormatter setDateFormat:@"ss.SSS"];
NSTimeInterval secondsForNextHour = (60.0 - [[minutesFormatter stringFromDate:self] doubleValue])*60 - [[secondsFormatter stringFromDate:self] doubleValue];
return secondsForNextHour;
}