如何快速显示年、日格式的秒数 [英] How to display seconds in year, day format in swift

查看:50
本文介绍了如何快速显示年、日格式的秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的就是更改 xcode 显示我的值的方式.我的代码当前以分钟为单位显示某物的年龄,例如 4503mint .我希望能够以以下格式显示这些值 3 days 3 hours 3 mint 而不是 4503 mint.真的很感谢你的帮助.问候

All i need to do is to change the way that xcode displaying my values in. My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint. Really appreciate your help. Regards

推荐答案

你说:

我的代码当前以分钟为单位显示某物的年龄,例如 4503mint .我希望能够以下列格式显示这些值 3 days 3 hours 3 mint 而不是 4503 mint.

My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint.

您可以使用 NSDateComponentsFormatter 对其进行格式化.只需将分钟数乘以 60 即可获得 NSTimeInterval,然后您就可以将其提供给格式化程序:

You can format this using NSDateComponentsFormatter. Just multiply the number of minutes by 60 to get the NSTimeInterval, and then you can supply that to the formatter:

let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full

let minutes = 4503
let timeInterval = NSTimeInterval(minutes * 60)
print(formatter.stringFromTimeInterval(timeInterval))

这将产生3 天 3 小时 3 分钟"(或任何适合该设备语言环境的格式).

That will produce "3 days, 3 hours, 3 minutes" (or in whatever format appropriate for the locale for that device).

请参阅 NSDateComponentsFormatter 参考更多信息.

这篇关于如何快速显示年、日格式的秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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