gpt4 book ai didi

ios - Swift 显示时间从 Date (NSDate) 开始

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:32 33 4
gpt4 key购买 nike

在一个单元格中,我想显示来自 Parse 服务器中 NSDate 的时间。这是代码,但它不起作用。什么都没有改变,数据也没有被解析。

    if let createdat = (object?["createdAt"] as? String){
let pastDate = Date(timeIntervalSinceNow: TimeInterval(createdat)!)
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}


extension Date {
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))

let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day

if secondsAgo < minute {
return "\(secondsAgo) sec ago"
} else if secondsAgo < hour {
return "\(secondsAgo / minute) min ago"
} else if secondsAgo < day {
return "\(secondsAgo / hour) hrs ago"
} else if secondsAgo < week {
return "\(secondsAgo / day) days ago"
}

return "\(secondsAgo / week) weeks ago"
}
}

最佳答案

If you just want a Time Ago extension for Date go to the bottom of the answer 😊

我将向您展示一个示例,只是为了在几秒钟前和之后显示您的扩展更新。

注意:如果需要,您可以直接使用 Pase 中的日期:

if let pastDate = (object?["createdAt"] as? Date) {
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}

Swift 5.1

如何使用 Swift 5.1 显示秒前的示例:

自 iOS13 Apple 引入了一个新类 RelativeDateTimeFormatter

extension Date {
func timeAgoDisplay() -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: self, relativeTo: Date())
}
}

这个类将允许你根据你的语言获得一个时间之前的字符串。它会根据您的间隔自动选择正确的时间单位,这是一个示例:

|--------------------------|------------------|
| Time interval in seconds | Display |
|--------------------------|------------------|
| -6 | 6 seconds ago |
| -60 | 1 minute ago |
| -600 | 10 minutes ago |
| -6000 | 1 hour ago |
| -60000 | 16 hours ago |
|--------------------------|------------------|

您会注意到它会自动为您处理复数。

Swift 3Swift 4

如何使用 Swift 3Swift 4 获取秒前的示例:

首先:要获得秒数之前,我们需要检查是否有一分钟或更短的时间,要获得当前日期减去一分钟,您可以这样写:

let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!

第二:现在比较两个日期! (如果是您的扩展,我们会自行替换 yourDate)并获取这 2 个日期之间的差异。

if (minuteAgo < yourDate) {
let diff = Calendar.current.dateComponents([.second], from: yourDate, to: Date()).second ?? 0
print("\(diff) sec ago")
}

就这样,现在你可以打印时间了!

所以你的扩展是这样的:(这是获取时间的简单扩展)

extension Date {
func timeAgoDisplay() -> String {

let calendar = Calendar.current
let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!
let hourAgo = calendar.date(byAdding: .hour, value: -1, to: Date())!
let dayAgo = calendar.date(byAdding: .day, value: -1, to: Date())!
let weekAgo = calendar.date(byAdding: .day, value: -7, to: Date())!

if minuteAgo < self {
let diff = Calendar.current.dateComponents([.second], from: self, to: Date()).second ?? 0
return "\(diff) sec ago"
} else if hourAgo < self {
let diff = Calendar.current.dateComponents([.minute], from: self, to: Date()).minute ?? 0
return "\(diff) min ago"
} else if dayAgo < self {
let diff = Calendar.current.dateComponents([.hour], from: self, to: Date()).hour ?? 0
return "\(diff) hrs ago"
} else if weekAgo < self {
let diff = Calendar.current.dateComponents([.day], from: self, to: Date()).day ?? 0
return "\(diff) days ago"
}
let diff = Calendar.current.dateComponents([.weekOfYear], from: self, to: Date()).weekOfYear ?? 0
return "\(diff) weeks ago"
}
}

要使用它,这非常简单:

var now = Date()
now.timeAgoDisplay()

关于ios - Swift 显示时间从 Date (NSDate) 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44086555/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com