SWIFT显示时间前开始日期(NSDate) [英] Swift display time ago from Date (NSDate)

查看:38
本文介绍了SWIFT显示时间前开始日期(NSDate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个单元格中,我希望在解析服务器中显示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"
    }
}

推荐答案

如果您只想延长日期的时间,请转到答案😊底部

我将向您展示一个示例,以便在几秒钟之前和之后显示您的扩展已更新。

注意:如果需要,您可以直接使用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())!
第二个:现在比较这两个日期!(在您延期的情况下,我们会将您的日期替换为您自己)并获取这两个日期之间的差值。

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()

这篇关于SWIFT显示时间前开始日期(NSDate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆