将 NSNumber (double) 值转换为时间

发布于 2024-08-01 16:37:38 字数 107 浏览 10 评论 0原文

我尝试将“898.171813964844”之类的值转换为 00:17:02 (hh:mm:ss)。

在 Objective C 中如何做到这一点?

感谢帮助!

i try to convert a value like "898.171813964844" into 00:17:02 (hh:mm:ss).

How can this be done in objective c?

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-08-08 16:37:38

最终解决方案:

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]);

Final solution:

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]);
江挽川 2024-08-08 16:37:38

假设您只对小时、分钟和秒感兴趣,并且输入值小于或等于 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];   

Assuming you are just interested in hours, minutes and seconds and that the input value is less or equal 86400 you could do something like this:

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];   
无悔心 2024-08-08 16:37:38

我知道答案已被接受,但这是我使用 NSDateFormatter 并考虑到时区的回应(意外添加了@Ben 的时区时间 [例如 GMT+4])

    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。

I know the answer has already been accepted, but here is my response using NSDateFormatter and taking into account timezone (to your timezone hours [eg. GMT+4] being unexpectedly added @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]]);

[side note] @phx: assuming 898.171813964844 is in seconds, this would represent 00:14:58 not 00:17:02.

清旖 2024-08-08 16:37:38
  1. 使用 -doubleValue 将 NSNumber 值转换为 NSTimeInterval
  2. 使用 +dateWithTimeIntervalSinceNow 将 NSTimeInterval 值转换为 NSDate:
  3. 使用 -descriptionWithCalendarFormat:timeZone 将 NSDate 转换为 NSString:区域设置:
  1. Convert your NSNumber value to a NSTimeInterval with -doubleValue
  2. Convert your NSTimeInterval value to a NSDate with +dateWithTimeIntervalSinceNow:
  3. Convert your NSDate to a NSString with -descriptionWithCalendarFormat:timeZone:locale:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文