如何将 NSTimeInterval(秒)转换为分钟
我得到了某个事件过去的秒
时间。 它存储在 NSTimeInterval 数据类型中。
我想将其转换为分钟
和秒
。
例如,我有:“326.4”秒,我想将其转换为以下字符串: “5:26”。
实现这一目标的最佳方法是什么?
谢谢。
I've got an amount of seconds
that passed from a certain event. It's stored in a NSTimeInterval
data type.
I want to convert it into minutes
and seconds
.
For example I have: "326.4" seconds and I want to convert it into the following string:
"5:26".
What is the best way to achieve this goal?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
斯威夫特2版本
Swift 2 version
我是如何在 Swift 中做到这一点的(包括将其显示为“01:23”的字符串格式):
How I did this in Swift (including the string formatting to show it as "01:23"):
请记住,最初的问题是关于字符串输出,而不是伪代码或单个字符串组件。
许多答案都缺少国际化问题,并且大多数都手动进行数学计算。 一切都那么 20 世纪......
不要自己做数学(Swift 4)
利用库
如果您确实想要单独的组件和可读性好的代码,请查看 SwiftDate:
感谢 @mickmaccallum 和 @polarwar 充分使用
DateComponentsFormatter
Remember that the original question is about a string output, not pseudo-code or individual string components.
Many answers are missing the internationalization issues, and most doing the math computations by hand. All just so 20th century...
Do not do the Math yourself (Swift 4)
Leverage on libraries
If you really want individual components, and pleasantly readable code, check out SwiftDate:
Credits to @mickmaccallum and @polarwar for adequate usage of
DateComponentsFormatter
因为它本质上是一个双精度...
除以 60.0 并提取整数部分和小数部分。
整数部分将是整数分钟。
再次将小数部分乘以 60.0。
结果将是剩余的秒数。
Since it's essentially a double...
Divide by 60.0 and extract the integral part and the fractional part.
The integral part will be the whole number of minutes.
Multiply the fractional part by 60.0 again.
The result will be the remaining seconds.
输出:
Outputs:
这是一个 Swift 版本:
可以像这样使用:
Here's a Swift version:
Can be used like this:
Brian Ramsay 的代码,去伪化后:
Brian Ramsay’s code, de-pseudofied:
如果您的目标是 iOS 8 或 OS X 10.10 或更高版本,这会变得容易得多。 新的
NSDateComponentsFormatter
类允许您将给定的NSTimeInterval
从其以秒为单位的值转换为本地化字符串以向用户显示。 例如:Objective-C
Swift
NSDateCompnentsFormatter
还允许此输出采用更长的形式。 更多信息可以在 NSHipster 的 NSFormatter 文章中找到。 根据您已经使用的类(如果不是NSTimeInterval
),向格式化程序传递一个NSDateComponents
实例或两个可能会更方便NSDate
对象,也可以通过以下方法完成。Objective-C
Swift
If you're targeting at or above iOS 8 or OS X 10.10, this just got a lot easier. The new
NSDateComponentsFormatter
class allows you to convert a givenNSTimeInterval
from its value in seconds to a localized string to show the user. For example:Objective-C
Swift
NSDateCompnentsFormatter
also allows for this output to be in longer forms. More info can be found in NSHipster's NSFormatter article. And depending on what classes you're already working with (if notNSTimeInterval
), it may be more convenient to pass the formatter an instance ofNSDateComponents
, or twoNSDate
objects, which can be done as well via the following methods.Objective-C
Swift
请原谅我是一个 Stack 处女...我不知道如何回复 Brian Ramsay 的答案...
对于 59.5 和 59.99999 之间的第二个值,使用 round 不起作用。 在此期间第二个值将为 60。 使用 trunc 代替...
Forgive me for being a Stack virgin... I'm not sure how to reply to Brian Ramsay's answer...
Using round will not work for second values between 59.5 and 59.99999. The second value will be 60 during this period. Use trunc instead...
所有这些看起来都比实际需要的更复杂! 这是将时间间隔转换为小时、分钟和秒的一种简短而甜蜜的方法:
请注意,当您将浮点数放入 int 中时,您会自动获得 Floor(),但您可以将其添加到前两个 if if 让您感觉更好的 :-)
All of these look more complicated than they need to be! Here is a short and sweet way to convert a time interval into hours, minutes and seconds:
Note when you put a float into an int, you get floor() automatically, but you can add it to the first two if if makes you feel better :-)
伪代码:
pseudo-code:
简要说明
使用 NSCalendar 方法:
(NSDateComponents *)组件:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate 选项:(NSUInteger)opts
“作为使用指定组件的 NSDateComponents 对象,返回两个提供的日期之间的差异”。 来自 API 文档。
创建2个NSDate,其差值是要转换的NSTimeInterval。 (如果您的 NSTimeInterval 来自比较 2 个 NSDate,则不需要执行此步骤,甚至不需要 NSTimeInterval)。
从 NSDateComponents 获取您的报价
示例代码
已知问题
Brief Description
Use NSCalendar method:
(NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts
"Returns, as an NSDateComponents object using specified components, the difference between two supplied dates". From the API documentation.
Create 2 NSDate whose difference is the NSTimeInterval you want to convert. (If your NSTimeInterval comes from comparing 2 NSDate you don't need to do this step, and you don't even need the NSTimeInterval).
Get your quotes from NSDateComponents
Sample Code
Known issues