iOS格式字符串分为秒和秒 [英] iOS Format String into minutes and seconds
本文介绍了iOS格式字符串分为秒和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到了来自YouTube JSONC api的字符串,但持续时间是一个完整的数字,即2321而不是23:21或2而不是0:02。我该如何解决此问题?
I am receiving a string from the YouTube JSONC api, but the duration is coming as a full number i.e 2321 instead of 23:21 or 2 instead of 0:02. How would I go about fixing this?
编辑:
int duration = [videos valueForKey:@"duration"];
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
推荐答案
假设持续时间值实际上是以秒为单位的持续时间,然后你可以计算分钟数和秒数,然后将它们格式化为一个字符串。
Assuming the duration value is really the duration in seconds, then you can calculate the number of minutes and seconds and then format those into a string.
int duration = ... // some duration from the JSON
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
这篇关于iOS格式字符串分为秒和秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文