我尝试将像“898.171813964844”这样的值转换为00:17:02(hh:mm:ss)。
如何在目标c中完成?
感谢您的帮助!
答案 0 :(得分:31)
最终解决方案:
NSNumber *time = [NSNumber numberWithDouble:([online_time doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];
NSDate *online = [NSDate date];
online = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
答案 1 :(得分:9)
假设您只对小时,分钟和秒感兴趣并且输入值小于或等于86400,您可以执行以下操作:
NSNumber *theDouble = [NSNumber numberWithDouble:898.171813964844];
int inputSeconds = [theDouble intValue];
int hours = inputSeconds / 3600;
int minutes = ( inputSeconds - hours * 3600 ) / 60;
int seconds = inputSeconds - hours * 3600 - minutes * 60;
NSString *theTime = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
答案 2 :(得分:3)
我知道答案已经被接受了,但是我的回答是使用NSDateFormatter并考虑到时区(到你的时区时间[例如GMT + 4]被意外添加@Ben)
NSTimeInterval intervalValue = 898.171813964844;
NSDateFormatter *hmsFormatter = [[NSDateFormatter alloc] init];
[hmsFormatter setDateFormat:@"HH:mm:ss"];
[hmsFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"formatted date: %@", [hmsFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:intervalValue]]);
[旁注] @phx:假设898.171813964844以秒为单位,这将代表00:14:58而不是00:17:02。
答案 3 :(得分:1)
-doubleValue
+dateWithTimeIntervalSinceNow:
-descriptionWithCalendarFormat:timeZone:locale: