-3

我有一首歌的持续时间以秒为单位。

经过以下计算,我得到了以小时、分钟和秒为单位的时间。

int hour = time / 3600;
int minute = (time / 60) % 60;
int second = time % 60;

我需要用这种格式 HH:mm:ss 向他们展示

我怎样才能做到这一点?

4

6 回答 6

3

这可能会有所帮助

- (NSString *)timeFormatted:(int)totalSeconds
{

    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds / 60) % 60; 
    int hours = totalSeconds / 3600; 

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; 
}
于 2013-09-03T06:40:36.137 回答
2

根据您的描述,您不需要使用 NSDateFormatter

int hour = time / 3600;
int minute = (time / 60) % 60;
int second = time % 60;

NSString *yourDate = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second, null];
于 2013-09-03T06:39:59.127 回答
2

这将附加 0 并使其恰好是 2 个字符。

NSString *str = [NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,second ];
于 2013-09-03T06:40:11.877 回答
1

试试看

int hour = time / 3600;
int minute = (time / 60) % 60;
int second = time % 60;

 NSString *dateStr = [NSString stringWithFormat:@"%d %d %d",hour,minute,second];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"HH:mm:ss"];
    NSDate *date1 = [dateFormat dateFromString:dateStr];

希望它会帮助你。

于 2013-09-03T06:37:48.997 回答
0

试试这个代码

   float seconds = totalSeconds % 60; 
   float minutes = (totalSeconds / 60) % 60; 
   float hours = totalSeconds / 3600; 
   return [NSString stringWithFormat:@"%02f:%02f:%02f",hours, minutes, seconds];
于 2013-09-03T06:43:26.327 回答
0
NSString *time = [NSString stringWithFormat:@"%d:%d:%d", hour, minute, second];
于 2013-09-03T06:40:48.323 回答