我有几秒钟的歌曲持续时间。
经过以下计算,我有时间,小时,分钟和秒。
int hour = time / 3600;
int minute = (time / 60) % 60;
int second = time % 60;
我需要用这种格式显示它们HH:mm:ss
我该怎么做?
答案 0 :(得分: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];
}
答案 1 :(得分:2)
根据您的描述
,您无需使用NSDateFormatterint hour = time / 3600;
int minute = (time / 60) % 60;
int second = time % 60;
NSString *yourDate = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second, null];
答案 2 :(得分:2)
这将追加0并使其恰好为2个字符..
NSString *str = [NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,second ];
答案 3 :(得分: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];
希望它会对你有所帮助。
答案 4 :(得分:0)
NSString *time = [NSString stringWithFormat:@"%d:%d:%d", hour, minute, second];
答案 5 :(得分:0)
试试此代码
float seconds = totalSeconds % 60;
float minutes = (totalSeconds / 60) % 60;
float hours = totalSeconds / 3600;
return [NSString stringWithFormat:@"%02f:%02f:%02f",hours, minutes, seconds];