gpt4 book ai didi

ios - 将时间戳从 Firebase 转换为 Time Ago

转载 作者:行者123 更新时间:2023-12-03 18:09:26 24 4
gpt4 key购买 nike

当用户点击提要中的帖子时,我想显示每条评论的发布时间。我正在尝试将 Date().timeIntervalSince1970 值转换为发布时间的“x 时间前”。

在加载 View 之前,我为今天的日期声明了一个 now 变量。

let now = Date()

日期是在我为评论设置字典时指定的。

let commentValues: [String: Any] = [
"comment" : comment,
"uid": uid,
"commentDate": Date().timeIntervalSince1970
]

我正在获取这样的评论,我还调用了日期函数并将其分配给 Storyboard 中的日期标签。

var comments = [Comment]()
fileprivate func fetchComments() {

guard let postID = self.post?.id else {return}
let commentRef = Database.database().reference().child("comments").child(postID)
commentRef.observe(.childAdded, with: { (snapshot) in

guard let dict = snapshot.value as? [String: Any] else {return}
guard let comment = dict["comment"] as? String else {return}
guard let uid = dict["uid"] as? String else {return}
guard let commentDate = dict["commentDate"] as? Double else {return}


let userRef = Database.database().reference().child("users/\(String(describing: uid))/profile")
userRef.observe(.value, with: { snapshot in
guard let value = snapshot.value as? [String:Any] else {return}
let username = value["username"]
let uid = dict["uid"]
let email = value["email"]

let myTimeInterval = TimeInterval(commentDate)
let time = Date(timeIntervalSinceNow: myTimeInterval)

let userProfile = User(uid: uid as! String, username: username as! String, email: email as! String)
let comment = Comment(uid: uid as! String, user: userProfile, comment: comment, time: time.timeAgoDisplay())
self.comments.append(comment)
self.commentCollectionView.reloadData()
})
})
}

我已经设置了一个日期扩展,我正在尝试计算日期的 timeAgo:

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
let month = 4 * week
let year = 12 * month

if secondsAgo < minute {
print("\(secondsAgo)s ago")
return "\(secondsAgo)s ago"
} else if secondsAgo < hour {
return "\(secondsAgo)m ago"
} else if secondsAgo < day {
return "\(secondsAgo)hr ago"
} else if secondsAgo < week {
return "\(secondsAgo)d ago"
} else if secondsAgo < month {
return "\(secondsAgo)w ago"
} else if secondsAgo < year {
return "\(secondsAgo)mo ago"
}
return "\(secondsAgo)yr ago"
}
}

正在显示日期,但我得到的结果是“-12983918s 前”。如何将时间显示为“2 秒前”或“1 小时前”或“1 周前”等?

最佳答案

下面是一个使用 Calendar.component 来区分可能的时间结果的示例。

if let timestamp = post?.timestamp {
print(timestamp)
let timestampDate = Date(timeIntervalSince1970: Double(timestamp))
let now = Date()
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth])
let diff = Calendar.current.dateComponents(components, from: timestampDate, to: now)

var timeText = ""
if diff.second! <= 0 {
timeText = "Now"
}
if diff.second! > 0 && diff.minute! == 0 {
timeText = (diff.second == 1) ? "\(diff.second!) second ago" : "\(diff.second!) seconds ago"
}
if diff.minute! > 0 && diff.hour! == 0 {
timeText = (diff.minute == 1) ? "\(diff.minute!) minute ago" : "\(diff.minute!) minutes ago"
}
if diff.hour! > 0 && diff.day! == 0 {
timeText = (diff.hour == 1) ? "\(diff.hour!) hour ago" : "\(diff.hour!) hours ago"
}
if diff.day! > 0 && diff.weekOfMonth! == 0 {
timeText = (diff.day == 1) ? "\(diff.day!) day ago" : "\(diff.day!) days ago"
}
if diff.weekOfMonth! > 0 {
timeText = (diff.weekOfMonth == 1) ? "\(diff.weekOfMonth!) week ago" : "\(diff.weekOfMonth!) weeks ago"
}

timeLabel.text = timeText
}

此示例附加了“帖子”的时间戳...但您可以对您的评论使用相同的逻辑。试一试,如果您有任何问题,请告诉我。干杯!

编辑:包括有关 Calendar.component 的文档 https://developer.apple.com/documentation/foundation/calendar.component

关于ios - 将时间戳从 Firebase 转换为 Time Ago,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50258591/

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