Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

time - iOS Format String into minutes and seconds

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?

JSON C

EDIT:

int duration = [videos valueForKey:@"duration"];
int minutes = duration / 60;
int seconds = duration % 60;

NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...