- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将时间从秒转换为位置格式,例如 2:05 min 或 1:23 h 或 19 s。我在检索本地化时间缩写单位时遇到问题。这是我的代码。
let secs: Double = 3801
let stopWatchFormatter = DateComponentsFormatter()
stopWatchFormatter.unitsStyle = .positional
stopWatchFormatter.maximumUnitCount = 2
stopWatchFormatter.allowedUnits = [.hour, .minute, .second]
print(stopWatchFormatter.string(from: secs)) // 1:03
stopWatchFormatter.unitsStyle = .short
print(stopWatchFormatter.string(from: secs)) // 1 hr, 3 min
如您所见,3801 秒的格式设置为 1:03,这很好,但我不知道 DateComponentsFormatter
使用的是小时还是分钟等。我可能会使用简单的 MOD 逻辑来检查它,但接下来是本地化的困难部分。另请注意,如果我将 collapsesLargestUnit
设置为 false
,MOD 解决方案将毫无值(value)。
最佳答案
DateComponentsFormatter
不直接支持您想要的格式,它本质上是位置格式,但短格式的第一个单元显示在末尾。
以下辅助函数将这两个单独的结果组合成您想要的结果。这应该适用于任何语言环境,但需要进行彻底的测试以确认这一点。
func formatStopWatchTime(seconds: Double) -> String {
let stopWatchFormatter = DateComponentsFormatter()
stopWatchFormatter.unitsStyle = .positional
stopWatchFormatter.maximumUnitCount = 2
stopWatchFormatter.allowedUnits = [.hour, .minute, .second]
var pos = stopWatchFormatter.string(from: seconds)!
// Work around a bug where some values return 3 units despite only requesting 2 units
let parts = pos.components(separatedBy: CharacterSet.decimalDigits.inverted)
if parts.count > 2 {
let seps = pos.components(separatedBy: .decimalDigits).filter { !$0.isEmpty }
pos = parts[0..<2].joined(separator: seps[0])
}
stopWatchFormatter.maximumUnitCount = 1
stopWatchFormatter.unitsStyle = .short
let unit = stopWatchFormatter.string(from: seconds)!
// Replace the digits in the unit result with the pos result
let res = unit.replacingOccurrences(of: "[\\d]+", with: pos, options: [.regularExpression])
return res
}
print(formatStopWatchTime(seconds: 3801))
输出:
1:03 hr
关于iOS DateComponentsFormatter - 获取使用的单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230176/
我想打印,例如,值 1.5 为“1.5 秒”,就像 Safari 的时间线那样,我正在尝试使用 DateComponentsFormatter。 不幸的是,它的 .allowedUnits只能下降到
我想将时间从秒转换为位置格式,例如 2:05 min 或 1:23 h 或 19 s。我在检索本地化时间缩写单位时遇到问题。这是我的代码。 let secs: Double = 3801 let st
我正在使用内置的 DateComponentsFormatter 来计算当前日期和另一个日期之间的年数。如果日期过去太远,例如 1940,则返回的单位是错误的: let dateComponents
我使用 DateComponentsFormatter 在 iOS 中格式化 TimeInterval。如果我得到 1:09 的结果,则应显示前导零。结果如下所示:01:09。这是否可以通过 Date
当 DateComponentsFormatter 返回意外数量的单位时,我遇到了问题。有没有人遇到同样的问题? import Foundation let formatter = DateCompo
在回答this question关于如何基于比较两个日期创建“xxx ago”字符串,我写了一个完整的问题解决方案。 它涉及使用 DateComponentsFormatter 并将 maximumU
我在 Swift 中使用以下 DateComponentsFormatter: let formatter = DateComponentsFormatter() formatter.unitsSty
因此,我在需要仅格式化给定 TimeInterval 的纳秒数的情况下使用 DateComponentsFormatter。问题是它没有按预期工作,或者我在这里混淆了一些我不知道的东西。 这里有 3
我正在使用 DateComponentsFormatter 来实现类似的目标:“1 小时 30 分钟”。这是我的代码: let formatter = DateComponentsFormatter(
情况 我有一个函数,它使用 DateComponentFormatter 的函数 fun string(from: Date, to: Date) 返回一个基于两个时间差的格式化字符串日期,它完美地工
我有一个只有一种语言的应用程序。在显示自日期以来的时间间隔时,我想强制使用这种语言。例如。 “9 个月前”只是用一种特定的语言而不是英语。有没有一种方法可以使用 DateComponentsForma
我在我的项目中使用 swift 并且我想显示这样的特定日期:5 天前或 5 个月前或 ... 我正在使用 DateComponentsFormatter 并且它运行良好,但问题是我想像“昨天”一样显示
我是一名优秀的程序员,十分优秀!