几小时,分钟,秒的NSNumber

时间:2022-07-08 21:31:32

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.

我正在努力做一些应该很容易的事情。我的NSNumber值为32025.89秒。我需要以小时,分钟,秒表示。有没有一种吐出来的方法?我似乎找不到合适的格式化程序。

4 个解决方案

#1


8  

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

您是否尝试过创建NSDate并使用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.

如果您需要小时,分钟和秒的单个值,请使用nall建议的NSDateComponents。

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.

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

#2


9  

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).

如果您不想将其分开,请尝试这样做。它可能足够接近您的目的。注意:它不考虑亚秒精度。 (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]);

#3


4  

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

这是一种没有任何库的方法,除了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;

#4


2  

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

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

#1


8  

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

您是否尝试过创建NSDate并使用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.

如果您需要小时,分钟和秒的单个值,请使用nall建议的NSDateComponents。

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.

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

#2


9  

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).

如果您不想将其分开,请尝试这样做。它可能足够接近您的目的。注意:它不考虑亚秒精度。 (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]);

#3


4  

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

这是一种没有任何库的方法,除了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;

#4


2  

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

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