将秒格式化为 hh:ii:ss
我有一个基本计时器应用程序。它跟踪应用程序运行的秒数。我想将其转换为秒(NSUInteger)显示为:00:00:12 (hh:mm:ss)。所以我读过这篇文章:
我从中编写了这段代码:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self meeting] elapsedSeconds]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
它工作正常,但它从 04:00:00 开始。我不知道为什么。我还尝试做类似的事情:
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:[[self meeting] elapsedSeconds] * -1];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
认为它会正确显示计数器,但它执行了一个奇怪的 01:23:00,然后只是翻转到 04:00:00 并在其余时间停留在那里。
多发性硬化症
I have app that is a basic timer. It tracks the number of seconds the app has run. I want to convert it so the seconds (NSUInteger) are displayed like: 00:00:12 (hh:mm:ss). So I've read this post:
NSNumber of seconds to Hours, minutes, seconds
From which I wrote this code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self meeting] elapsedSeconds]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
It works fine, but it starts out with 04:00:00. I'm not sure why. I also tried doing something like:
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:[[self meeting] elapsedSeconds] * -1];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];
Thinking that it would display the counter correctly, but it does a wierd 01:23:00, then just flops to 04:00:00 and stays there for the rest of the time.
MS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这类似于 上一个答案关于格式化时间,但不需要日期格式化程序,因为我们不再处理日期。
如果您将秒数存储为整数,您可以自己计算出各个时间分量:
This is similar to a previous answer about formatting time but doesn't require a date formatter because we aren't dealing with dates any more.
If you have the number of seconds stored as an integer, you can work out the individual time components yourself:
虽然有更简单的方法可以做到这一点(@dreamlax 有一个非常好的方法),但让我解释一下您的示例有什么问题,然后让我们让它工作:
首先,它显示
04:00:00< 的原因/code> (嗯,它实际上可能显示
04:00:12
)是因为它将时间从 UTC/GMT 转换为您的本地时间。要解决此问题,您需要添加以下行:然后,它将不再显示
04:00:12
,因为它不会转换时区。不幸的是,它现在将显示12:00:12
而不是00:00:12
,因为现在是午夜。为了解决这个问题,请使用HH
格式化程序而不是hh
将字符串转换为 24 小时时间:请记住,因为这是设计用于有时,它不会工作超过 24 小时(因为它会再次“滚动”到午夜)。
完整的代码是:
While there are easier ways of doing this (@dreamlax has a very good way), let me explain what is wrong with your example and let's get it working:
First, the reason that it is showing
04:00:00
(well, it is probably actually showing04:00:12
) is because it is converting the time from UTC/GMT to your local time. To fix this, you need to add the following line:Then, it will no longer show
04:00:12
because it doesn't convert the timezone. Unfortunately, it will now show12:00:12
instead of00:00:12
because it is midnight. In order to fix that, have it convert the string to 24 hour time instead by using theHH
formatter instead ofhh
:Keep in mind that since this was designed to work with times, that it will not work for more than 24 hours (because it will "roll over" to midnight again).
The full code would be: