NS秒数到小时,分钟,秒 [英] NSNumber of seconds to Hours, minutes, seconds

查看:138
本文介绍了NS秒数到小时,分钟,秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可怕的时间试图做一些应该很容易的事情。我有一个NSNumber值32025.89秒。我需要在小时,分钟,秒表示。有没有一种方法吐出来?我似乎找不到一个合适的格式化。

解决方案

您尝试从中创建 NSDate NSDateFormatter

  NSDate * date = [NSDate dateWithTimeIntervalSince1970: doubleValue]]; 
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@HH:mm:ss];
NSLog(@%@,[formatter stringFromDate:date]);
[formatter release];

如果您需要小时,分钟和秒的单个值,请使用

如果 NSNumber ,上面的命令不会输出正确的金额$ c>表示超过一天。它可以是1天和1小时,它只打印01:00:00。在这种情况下,您应该手动计算小时,分钟和秒钟。


I am having a terrible time trying to do something that should be easy. I have a NSNumber value of 32025.89 seconds. I need to represent that in Hours, Minutes, Seconds. Is there a method that spits that out? I can't seem to find a proper formatter.

解决方案

Have you tried creating an NSDate from that and printing that using an NSDateFormatter?

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[theNumber doubleValue]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSLog(@"%@", [formatter stringFromDate:date]); 
[formatter release];

If you need the individual values for hours, minutes, and seconds, use NSDateComponents as suggested by nall.

The above won't print the correct amounts if the NSNumber represents more than one day. It could be 1 day and 1 hour, and it would only print 01:00:00. In such a case you should calculate hours, minutes and seconds manually.

这篇关于NS秒数到小时,分钟,秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆