将秒格式化为 hh:ii:ss

发布于 2024-09-29 19:42:20 字数 814 浏览 6 评论 0原文

我有一个基本计时器应用程序。它跟踪应用程序运行的秒数。我想将其转换为秒(NSUInteger)显示为:00:00:12 (hh:mm:ss)。所以我读过这篇文章:

NSNumber of秒到小时,分钟,秒< /a>

我从中编写了这段代码:

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 技术交流群。

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

发布评论

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

评论(2

嘦怹 2024-10-06 19:42:20

这类似于 上一个答案关于格式化时间,但不需要日期格式化程序,因为我们不再处理日期。

如果您将秒数存储为整数,您可以自己计算出各个时间分量:

NSUInteger h = elapsedSeconds / 3600;
NSUInteger m = (elapsedSeconds / 60) % 60;
NSUInteger s = elapsedSeconds % 60;

NSString *formattedTime = [NSString stringWithFormat:@"%u:%02u:%02u", h, m, s];

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:

NSUInteger h = elapsedSeconds / 3600;
NSUInteger m = (elapsedSeconds / 60) % 60;
NSUInteger s = elapsedSeconds % 60;

NSString *formattedTime = [NSString stringWithFormat:@"%u:%02u:%02u", h, m, s];
碍人泪离人颜 2024-10-06 19:42:20

虽然有更简单的方法可以做到这一点(@dreamlax 有一个非常好的方法),但让我解释一下您的示例有什么问题,然后让我们让它工作:

首先,它显示 04:00:00< 的原因/code> (嗯,它实际上可能显示 04:00:12)是因为它将时间从 UTC/GMT 转换为您的本地时间。要解决此问题,您需要添加以下行:

[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

然后,它将不再显示 04:00:12,因为它不会转换时区。不幸的是,它现在将显示 12:00:12 而不是 00:00:12,因为现在是午夜。为了解决这个问题,请使用 HH 格式化程序而不是 hh 将字符串转换为 24 小时时间:

[formatter setDateFormat:@"HH:mm:ss"];

请记住,因为这是设计用于有时,它不会工作超过 24 小时(因为它会再次“滚动”到午夜)。

完整的代码是:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self meeting] elapsedSeconds]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSLog(@"%@", [formatter stringFromDate:date]);
// Results:  00:00:12

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 showing 04: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:

[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Then, it will no longer show 04:00:12 because it doesn't convert the timezone. Unfortunately, it will now show 12:00:12 instead of 00:00:12 because it is midnight. In order to fix that, have it convert the string to 24 hour time instead by using the HH formatter instead of hh:

[formatter setDateFormat:@"HH:mm:ss"];

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:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self meeting] elapsedSeconds]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSLog(@"%@", [formatter stringFromDate:date]);
// Results:  00:00:12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文