NS 秒数 至 小时、分钟、秒

发布于 2024-08-06 20:14:32 字数 100 浏览 11 评论 0原文

我在尝试做一些本应很容易的事情时遇到了困难。我的 NSNumber 值为 32025.89 秒。我需要用小时、分钟、秒来表示。有没有一种方法可以把它吐出来?我似乎找不到合适的格式化程序。

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.

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

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

发布评论

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

评论(4

橘味果▽酱 2024-08-13 20:14:32

如果你不想把它分开,试试这个。对于您的目的来说,它可能足够接近。 注意:它不考虑亚秒精度。 (setSecond 接受一个 NSInteger)。

NSDateComponents* c = [[[NSDateComponents alloc] init] autorelease];
[c setSecond:32025.89];

NSCalendar* cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                   autorelease];

NSDate* d = [cal dateFromComponents:c];

NSDateComponents* result = [cal components:NSHourCalendarUnit |
                                           NSMinuteCalendarUnit |
                                           NSSecondCalendarUnit
                                  fromDate:d];

NSLog(@"%d hours, %d minutes, %d seconds",
    [result hour], [result minute], [result second]);

If you don't want to just divide it out, try this. It may be close enough for your purposes. Note: It doesn't account for sub-second precision. (setSecond takes an NSInteger).

NSDateComponents* c = [[[NSDateComponents alloc] init] autorelease];
[c setSecond:32025.89];

NSCalendar* cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
                   autorelease];

NSDate* d = [cal dateFromComponents:c];

NSDateComponents* result = [cal components:NSHourCalendarUnit |
                                           NSMinuteCalendarUnit |
                                           NSSecondCalendarUnit
                                  fromDate:d];

NSLog(@"%d hours, %d minutes, %d seconds",
    [result hour], [result minute], [result second]);
鸠书 2024-08-13 20:14:32

您是否尝试过从中创建一个 NSDate 并使用 NSDateFormatter 打印它?

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

如果您需要小时、分钟和秒的单独值,请按照 nall 的建议使用 NSDateComponents

如果 NSNumber 代表超过一天,上面的代码将不会打印正确的金额。可能是 1 天 1 小时,并且只会打印 01:00:00。在这种情况下,您应该手动计算小时、分钟和秒。

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.

这是一种除了 math.h 中的 modf() 之外不需要任何库的方法:

float fsec = 32025.89f, frac = 0;
int milliseconds = (int)(modf(fsec, &frac) * 1000);
int isec = (int)frac;
int hours = isec / 3600;
int minutes = (isec % 3600) / 60;
int seconds = isec % 60;

Here's a way without any libraries except for modf() from math.h:

float fsec = 32025.89f, frac = 0;
int milliseconds = (int)(modf(fsec, &frac) * 1000);
int isec = (int)frac;
int hours = isec / 3600;
int minutes = (isec % 3600) / 60;
int seconds = isec % 60;
柏拉图鍀咏恒 2024-08-13 20:14:32

NSNumber *valueForDisplay = [NSNumber numberWithDouble:
[self valueForDisplay:clockName]];

NSNumber *totalDays = [NSNumber numberWithDouble:
         ([valueForDisplay doubleValue] / 86400)];
NSNumber *totalHours = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 3600) -
          ([totalDays intValue] * 24))];
NSNumber *totalMinutes = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 60) -
         ([totalDays intValue] * 24 * 60) -
         ([totalHours intValue] * 60))];
NSNumber *totalSeconds = [NSNumber numberWithInt:
         ([valueForDisplay intValue] % 60)];

NSNumber *valueForDisplay = [NSNumber numberWithDouble:
[self valueForDisplay:clockName]];

NSNumber *totalDays = [NSNumber numberWithDouble:
         ([valueForDisplay doubleValue] / 86400)];
NSNumber *totalHours = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 3600) -
          ([totalDays intValue] * 24))];
NSNumber *totalMinutes = [NSNumber numberWithDouble:
         (([valueForDisplay doubleValue] / 60) -
         ([totalDays intValue] * 24 * 60) -
         ([totalHours intValue] * 60))];
NSNumber *totalSeconds = [NSNumber numberWithInt:
         ([valueForDisplay intValue] % 60)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文