我已经有一段时间了,现在试图解决这个问题,但没有运气。
简而言之,我想采用这样的字符串:"2011-11-21 11:20:00"
并计算"Time Until".
在"1 day 36 mins"
这样的格式中,我可以在标签中显示一个字符串。
我无法绕过这个。以前任何人都有一些示例代码吗?任何帮助将不胜感激。
答案 0 :(得分:3)
@Maudicus走在正确的轨道上,但他的答案有一些缺陷。
@"yyyy-MM-dd HH:mm:ss"
获得日期后,您应该使用-[NSCalendar components:fromDate:toDate:options:]
来确定一个日期与另一个日期之间的差异。例如:
NSDate *date = ...; // the date converted using the date formatter
NSDate *target = [NSDate date]; // now
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger components = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *difference = [calendar components:components fromDate:date toDate:target options:0];
NSLog(@"difference: %d days, %d hours, %d minutes, %d seconds", [difference days], [difference hours], [difference minutes], [difference seconds]);
答案 1 :(得分:2)
此代码可以帮助您。
NSDate * date;
//Assume dateString is populated and of format NSString * dateString =@"2011-11-21 11:20:00";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
//EDITED date formatter to correct behavior
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
if (dateString != nil) {
date = [dateFormatter dateFromString:dateString];
}
[dateFormatter release];
NSTimeInterval difference = [date timeIntervalSinceDate:[NSDate date]];
int days = difference/(3600*24);
difference -= (float) (days*3600*24);
int hours = difference/(3600);
difference -= (float) (hours*3600);
int minutes = difference/(60);
NSString * timeRemaining = [NSString stringWithFormat:@"%dd %dh %dm", days, hours, minutes];
答案 2 :(得分:1)
duration.text = [NSString stringWithFormat:@"%d:%02d", (int)audioPlayer.duration / 3600, (int)audioPlayer.duration % 60, nil];