- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在回答this question关于如何基于比较两个日期创建“xxx
它涉及使用 DateComponentsFormatter
并将 maximumUnitCount
设置为 1 并将 unitsStyle
设置为 .full
。下面是创建 DateComponentsFormatter
的代码:
var timeFormatter:DateComponentsFormatter = {
let temp = DateComponentsFormatter()
temp.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
temp.maximumUnitCount = 1
temp.unitsStyle = .full
return temp
}()
然后我写了一个函数,它使用 DateComponentsFormatter 来输出“xxx units ago”字符串:
//Use our DateComponentsFormatter to generate a string showing "n <units> ago" where units is the largest units of the difference
//Between the now date and the specified date in te past
func timefromDateToNow(_ pastDate: Date) -> String {
if let output = timeFormatter.string(from: pastDate, to: now) {
return output + " ago"
} else {
return "error"
}
}
我用这样的代码计算过去的日期:
let value = Double.random(in: min ... max)
let past = now.addingTimeInterval(value)
奇怪的是,对于 value
的某些值,我得到了字符串“0 months ago”。我能够捕获的值是 -408754.0
,大约是 4 天 17 小时。
为什么调用 timeFormatter.string(from: Date(timeIntervalSinceNow: -408754.0), to: Date())
会返回 0 个月的字符串?它应该显示“4 天”的结果!
最佳答案
如果你想引用多久以前,考虑RelativeDateTimeFormatter
,例如
let formatter = RelativeDateTimeFormatter()
formatter.dateTimeStyle = .named
formatter.unitsStyle = .spellOut
formatter.formattingContext = .beginningOfSentence
let date = Date()
let longTimeAgo = date.addingTimeInterval(-10_000_000)
print(formatter.localizedString(for: longTimeAgo, relativeTo: date)) // Three months ago
let veryLongTimeAgo = date.addingTimeInterval(-100_000_000)
print(formatter.localizedString(for: veryLongTimeAgo, relativeTo: date)) // Three years ago
关于ios - 我在 DateComponentsFormatter 中发现了错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65160424/
我想打印,例如,值 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 并且它运行良好,但问题是我想像“昨天”一样显示
我是一名优秀的程序员,十分优秀!