我正在尝试显示今年年底的天数,小时数,分钟数和秒数。
我的想法是在当前时间创建一个NSDate,在年底创建一个NSDate。然后减去它们。
阅读文档后,我看到了一个添加方法,但没有减去2个NSDates。
必须有办法让倒数计时器?
答案 0 :(得分:1)
试试这个:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@\"yyyy-MM-dd HH:mm:ss\"];
NSDate *endDate = [dateFormatter dateFromString:@"2013-01-01 00:00:00"];
基于JustSid的评论,这似乎是从这里开始的方式:
NSDateComponents *timeDifference = [[NSCalendar currentCalendar]
components: NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
fromDate:[NSDate date] toDate:endDate options:0];
int days = timeDifference.day;
int hours = timeDifference.hour;
int minutes = timeDifference.minute;
int seconds = timeDifference.second;
答案 1 :(得分:0)
尝试这样的事情:
- (NSString*) getTimeInterval:(NSDate*) start endTime:(NSDate*) end {
if([[NSString stringWithFormat:@"%@", end] isEqualToString:@"1970-01-01 00:00:00 +0000"]) {
end = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]];
}
NSTimeInterval interval = [end timeIntervalSinceDate: start];
int numberOfdays = floor(interval/86400);
int numberOfHours = floor((interval - numberOfdays*86400) / 3600);
int numberOfMinutes = floor((interval - numberOfdays*86400 - numberOfHours*3600) / 60);
return [NSString stringWithFormat:@"%d d %d h %d m", numberOfdays, numberOfHours, numberOfMinutes];
}