iOS 格式字符串转换为分钟和秒 [英] iOS Format String into minutes and seconds

查看:52
本文介绍了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?

JSON C

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆