Ios 将时间戳从Firebase转换为Time Ago
当用户单击提要中的帖子时,我想显示每个评论发布的时间。我正在尝试将Date().timeIntervalSince1970值转换为“x time ago”,以表示发布日期 在加载视图之前,我将为今天的日期声明一个now变量Ios 将时间戳从Firebase转换为Time Ago,ios,swift,firebase,time,timestamp,Ios,Swift,Firebase,Time,Timestamp,当用户单击提要中的帖子时,我想显示每个评论发布的时间。我正在尝试将Date().timeIntervalSince1970值转换为“x time ago”,以表示发布日期 在加载视图之前,我将为今天的日期声明一个now变量 let now = Date() 日期是在我为注释设置字典时指定的,如 let commentValues: [String: Any] = [ "comment" : comment, "uid": uid, "commen
let now = Date()
日期是在我为注释设置字典时指定的,如
let commentValues: [String: Any] = [
"comment" : comment,
"uid": uid,
"commentDate": Date().timeIntervalSince1970
]
我正在获取这样的注释,在这里我还调用了date函数,并将其分配给故事板中的日期标签
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()
})
})
}
我已经设置了一个日期扩展,我正在尝试计算日期的时间:
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"
}
}
延期日期{
func timeAgoDisplay()->字符串{
设secondsAgo=Int(Date().timeIntervalSince(self))
让分钟=60
让小时=60*分钟
让一天=24小时*
每周=7天
让月=4*周
让年份=12*个月
如果第二个Sago<分钟{
打印(“\(第二个Sago)s ago”)
返回“\(第二个Sago)s ago”
}否则,如果秒Sago<小时{
返回“\(secondsAgo)m ago”
}否则,如果第二个西米<天{
返回“\(secondsAgo)小时前”
}如果第二个西米<一周{
返回“\(secondsAgo)d ago”
}否则,如果第二个Sago<月{
返回“\(secondsAgo)w ago”
}否则,如果第二个Sago<年份{
返回“\(第二个Sago)mo ago”
}
返回“\(第二个Sago)年前”
}
}
正在显示日期,但我得到的结果是“-12983918s前”。如何将时间显示为“2s前”、“1h前”或“1w前”等?下面是一个使用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
}
如果let timestamp=post?.timestamp{
打印(时间戳)
让timestampDate=Date(timeIntervalSince1970:Double(timestamp))
让我们现在=日期()
let components=Set([.second、.minute、.hour、.day、.weekOfMonth])
让diff=Calendar.current.dateComponents(组件,从:timestampDate,到:now)
var timeText=“”
如果差异秒!0和差异分钟!==0{
timeText=(diff.second==1)?“\(diff.second!)秒前”:“\(diff.second!)秒前”
}
如果差异分钟!>0和差异小时!==0{
timeText=(diff.minute==1)?“\(diff.minute!)分钟前”:“\(diff.minute!)分钟前”
}
如果差异小时数!>0和差异日数!==0{
timeText=(diff.hour==1)?“\(diff.hour!)小时前”:“\(diff.hour!)小时前”
}
如果差异日!>0和差异周月!==0{
timeText=(diff.day==1)?“\(diff.day!)日前”:“\(diff.day!)日前”
}
如果差异周数月数!>0{
timeText=(diff.weekOfMonth==1)?“\(diff.weekOfMonth!)周前”:“\(diff.weekOfMonth!)周前”
}
timeLabel.text=timeText
}
本例附加了“post”的时间戳……但是您可以对注释使用相同的逻辑。试一试,如果你有任何问题,请告诉我。干杯
编辑:包括Calendar.component上的文档
您需要将
secondsAgo
转换为second、date、month、year、…:
这里有一个想法:
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
var result = ""
if secondsAgo >= 60 {
let minutesAgo = secondsAgo / 60
if minutesAgo >= 60 {
let hoursAgo = minutesAgo / 60
if hoursAgo >= 60 {
let daysAgo = hoursAgo / 24
if daysAgo >= 30 {
let monthsAgo = daysAgo / 12
if monthsAgo >= 12 {
let yearsAgo = monthsAgo / 12
result = "\(yearsAgo)y ago"
} else { // months < 12
result = "\(monthsAgo)mo ago"
}
} else { // days < 30
result = "\(daysAgo)d ago"
}
} else { // hours < 24
result = "\(hoursAgo)h ago"
}
} else { // minutes < 60ms
result = "\(minutesAgo)m ago"
}
} else { // seconds < 60s
result = "\(secondsAgo)seconds ago"
}
return result
}
func timeAgoDisplay()->字符串{
设secondsAgo=Int(Date().timeIntervalSince(self))
var result=“”
如果secondsAgo>=60{
让minutesAgo=第二个Sago/60
如果分钟前>=60{
让hoursAgo=分钟前/60
如果hoursAgo>=60{
让daysAgo=小时数/24
如果daysAgo>=30{
让monthsAgo=daysAgo/12
如果月数>=12{
let yearsAgo=月前/12
结果=“\(年)年前”
}其他{//12个月
结果=“\(月)个月前”
}
}否则{//天<30天
结果=“\(daysAgo)d年前”
}
}否则{//小时<24小时
结果=“\(hoursAgo)小时前”
}
}否则{//分钟<60毫秒
结果=“\(分钟前)m前”
}
}否则{//秒<60秒
result=“\(secondsAgo)秒前”
}
返回结果
}
希望这个有用的你可以使用calendar.component…我会在有机会的时候发布一个例子。干杯非常感谢你!今年怎么样<代码>其他,如果不同年份!>0{timeText=(diff.year==1)?“\(diff.year!)y ago”:“\(diff.year!)y ago”}是否就是这样?请查看此文档。它有可能的时间范围的完整列表。