将秒整数转换为 HH:MM, iPhone [英] Convert Seconds Integer To HH:MM, iPhone

查看:17
本文介绍了将秒整数转换为 HH:MM, iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此苦苦挣扎.我有一个值以秒为单位显示在 HH:MM 格式的标签中.我已经在互联网上搜索了很长时间并找到了一些答案,但要么没有完全理解它们,要么它们看起来像是做我想做的事情的奇怪方式.如果有人能帮我解决这个问题,那就太好了!请记住,我是这个游戏的新手,所以这个问题对于更有经验的人来说似乎是一个非常基本的问题.

I am struggling with this. I have a value in seconds that I want to display in a label in HH:MM format. I have searched the internet for ages and found some answers, but either not fully understood them, or they seem like an odd way of doing what I want. If someone could help me out on this one that would be great! Bear in mind that I am new to this games so this question may seem like a really basic one to the more experienced out there.

推荐答案

我一直在寻找与您正在寻找的相同的东西,但没有找到.所以我写了一个 -

I was looking for the same thing that you are looking but couldn't find one. So I wrote one -

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

Swift 中也能完美运行:

works perfectly in Swift as well:

 func timeFormatted(totalSeconds: Int) -> String {
    let seconds: Int = totalSeconds % 60
    let minutes: Int = (totalSeconds / 60) % 60
    let hours: Int = totalSeconds / 3600
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
 }

这篇关于将秒整数转换为 HH:MM, iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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