在一个单元格中,我想显示解析服务器中 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 显示几秒前的示例:
自iOS13苹果引入了一个新类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 3 或 Swift 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()
自 iOS 13 起,您可以使用 Apple 的 RelativeDateFormatter。优点是生成的字符串已本地化。
let date = Date().addingTimeInterval(-15000)
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
let string = formatter.localizedString(for: date, relativeTo: Date())
print(string) // 4 hours ago
例如参见这篇博客文章。
这是 Swift 5 中的解决方案:
extension Date {
func timeAgo() -> 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 quotient: Int
let unit: String
if secondsAgo < minute {
quotient = secondsAgo
unit = "second"
} else if secondsAgo < hour {
quotient = secondsAgo / minute
unit = "min"
} else if secondsAgo < day {
quotient = secondsAgo / hour
unit = "hour"
} else if secondsAgo < week {
quotient = secondsAgo / day
unit = "day"
} else if secondsAgo < month {
quotient = secondsAgo / week
unit = "week"
} else {
quotient = secondsAgo / month
unit = "month"
}
return "\(quotient) \(unit)\(quotient == 1 ? "" : "s") ago"
}
}
有点棘手)
基本上你需要
iOS 提供了几种方法来做到这一点
DateComponentsFormatter
+: 无法处理本地化和其他与字符串相关的内容
-: 不太灵活
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .brief
formatter.zeroFormattingBehavior = .dropAll
let result = formatter.string(from: Date().advanced(by: -300), to: Date())
result
结果:
“5分钟”
RelativeDateTimeFormatter
+: 无法处理本地化和其他与字符串相关的内容
-: iOS13+
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: self, relativeTo: Date())
定制方式
+: 灵活
-: 编码一切
样品:
import Foundation
public extension Date {
struct DetailedDateSuffix {
let year: String
let month: String
let week: String
let day: String
let hour: String
let min: String
let second: String
let quotient: String
let suffix: String
public init(year: String,
week: String,
month: String,
day: String,
hour: String,
min: String,
second: String,
quotient: String,
suffix: String = String.empty) {
self.year = year
self.month = month
self.week = week
self.day = day
self.hour = hour
self.min = min
self.second = second
self.quotient = quotient
self.suffix = suffix
}
}
func toDetailedReadableFormat(_ suffix: DetailedDateSuffix) -> String {
if #available(iOS 13.0, *) {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: self, relativeTo: Date())
} else {
let calendar = Calendar.current
let ageComponents = calendar.dateComponents(
[
.year,
.month,
.weekOfYear,
.day,
.hour,
.minute,
.second
],
from: self,
to: Date())
var description: String = String.empty
if let years = ageComponents.year,
let months = ageComponents.month,
let weeks = ageComponents.weekOfYear,
let days = ageComponents.day,
let hours = ageComponents.hour,
let min = ageComponents.minute,
let sec = ageComponents.second {
var requireQuotient = false
if years > 0 {
description = "\(years)" + suffix.year
requireQuotient = years == 1
} else if months > 0 {
description = "\(months)" + suffix.month
requireQuotient = months == 1
} else if weeks > 0 {
description = "\(weeks)" + suffix.week
requireQuotient = weeks == 1
} else if days > 0 {
description = "\(days)" + suffix.day
requireQuotient = days == 1
} else if hours > 0 {
description = "\(hours)" + suffix.hour
requireQuotient = hours == 1
} else if min > 0 {
description = "\(min)" + suffix.min
requireQuotient = min == 1
} else if sec > 0 {
description = "\(sec)" + suffix.second
requireQuotient = sec == 1
}
description = requireQuotient ? "\(description)\(suffix.quotient)" : description
description = "\(description)\(suffix.suffix)"
}
return description
}
}
}
以字符串形式传递时间,格式例如: 2019-02-25 10:20:21 在变量 dateFormat 中传递日期格式
let dateFormat = "yyyy-MM-dd HH:mm:ss"
func timeInterval(timeAgo:String) -> String
{
let df = DateFormatter()
df.dateFormat = dateFormat
let dateWithTime = df.date(from: timeAgo)
let interval = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: dateWithTime!, to: Date())
if let year = interval.year, year > 0 {
return year == 1 ? "\(year)" + " " + "year ago" : "\(year)" + " " + "years ago"
} else if let month = interval.month, month > 0 {
return month == 1 ? "\(month)" + " " + "month ago" : "\(month)" + " " + "months ago"
} else if let day = interval.day, day > 0 {
return day == 1 ? "\(day)" + " " + "day ago" : "\(day)" + " " + "days ago"
}else if let hour = interval.hour, hour > 0 {
return hour == 1 ? "\(hour)" + " " + "hour ago" : "\(hour)" + " " + "hours ago"
}else if let minute = interval.minute, minute > 0 {
return minute == 1 ? "\(minute)" + " " + "minute ago" : "\(minute)" + " " + "minutes ago"
}else if let second = interval.second, second > 0 {
return second == 1 ? "\(second)" + " " + "second ago" : "\(second)" + " " + "seconds ago"
} else {
return "a moment ago"
}
}
let now = Date()
let pastDate = Date(timeIntervalSinceNow: -60 * 60 * 24)
enum DisplayTime {
case short
case long
var seconds: String {
switch self {
case .short: return "s"
case .long: return "seconds"
}
}
var minutes: String {
switch self {
case .short: return "m"
case .long: return "minutes"
}
}
var hours: String {
switch self {
case .short: return "h"
case .long: return "hours"
}
}
var days: String {
switch self {
case .short: return "d"
case .long: return "days"
}
}
var weeks: String {
switch self {
case .short: return "w"
case .long: return "weeks"
}
}
}
extension Date {
func timeAgoDisplay(_ display: DisplayTime) -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
switch secondsAgo {
case let seconds where seconds < minute : return "\(secondsAgo) \(display.seconds) ago"
case let seconds where seconds < hour: return "\(secondsAgo / minute) \(display.minutes) ago"
case let seconds where seconds < day: return "\(secondsAgo / hour) \(display.hours) ago"
case let seconds where seconds < week: return "\(secondsAgo / day) \(display.days) ago"
default: "\(secondsAgo / week) \(display.weeks) ago"
}
return "\(secondsAgo / week) \(display.weeks) ago"
}
}
pastDate.timeAgoDisplay(.short)
终于在swift4.2中为我找到了一个简单的解决方案
let start = //Enter Start Date here....
let end = Date()
let formatter = DateComponentsFormatter()
formatter.maximumUnitCount = 2
formatter.unitsStyle = .full
formatter.allowedUnits = [.year, .month, .day]
let timeDifference = form.string(from: start, to: end)
print(timeDifference)
if timeDifference == "0 days"{
print("Today")
}
else if timeDifference == "1 days"{
print("\(timeDifference!)day ago")
}
else{
print("\(timeDifference!)days ago")
}
如果您只有 1970 年以来的时间戳。您可以使用此函数返回之前的时间。
private func timestampToStringAgo(timestamp: Int64) -> String{
let actualTime = Int64(Date().timeIntervalSince1970*1000)
var lastSeenTime = actualTime - timestamp
lastSeenTime /= 1000 //seconds
var lastTimeString = ""
if lastSeenTime < 60 {
if lastSeenTime == 1 {
lastTimeString = String(lastSeenTime) + " second ago"
} else {
lastTimeString = String(lastSeenTime) + " seconds ago"
}
} else {
lastSeenTime /= 60
if lastSeenTime < 60 {
if lastSeenTime == 1 {
lastTimeString = String(lastSeenTime) + " minute ago"
} else {
lastTimeString = String(lastSeenTime) + " minutes ago"
}
} else {
lastSeenTime /= 60
if lastSeenTime < 24 {
if lastSeenTime == 1 {
lastTimeString = String(lastSeenTime) + " hour ago"
} else {
lastTimeString = String(lastSeenTime) + " hours ago"
}
} else {
lastSeenTime /= 24
if lastSeenTime == 1 {
lastTimeString = String(lastSeenTime) + " day ago"
} else {
lastTimeString = String(lastSeenTime) + " days ago"
}
}
}
}
return lastTimeString
}
///This example rounds to "one unit", examples "27d" or "3m".
let _agoFormatter: DateComponentsFormatter = {
let f = DateComponentsFormatter()
f.unitsStyle = .abbreviated
f.allowedUnits = [.day, .hour, .minute]
f.maximumUnitCount = 1
return f
}()
只需使用各种值,例如
maximumUnitCount
即可获得您想要的。
let d = some Date
print( _agoFormatter.string(from: d, to: Date.now )
就是这样。
您可以使用
timeIntervalSince
来获取两个日期之间的秒数差异。然后将秒数与秒、分钟、小时、天、周、月和年进行比较,以确定正确的字符串后缀。
您可以使用此函数返回格式化的时间之前,只需向其传递一个日期和长(3天前)或短(3天前)的首选项。
请参阅我的完整指南:如何显示日期之前的时间
完整代码:
func getTimeSince(date: Date, isShort: Bool = false) -> String {
var formattedString = String()
let now = Date()
let secondsAgo = Int(now.timeIntervalSince(date))
let twoSeconds = 2
let minute = 60
let twoMinutes = minute * 2
let hour = 60 * minute
let twoHours = hour * 2
let day = 24 * hour
let twoDays = day * 2
let week = 7 * day
let twoWeeks = week * 2
let month = 4 * week
let twoMonths = month * 2
let year = 12 * month
let twoYears = year * 2
let secondString = isShort ? "s ago" : " second ago"
let secondsString = isShort ? "s ago" : " seconds ago"
let minuteString = isShort ? "m ago" : " minute ago"
let minutesString = isShort ? "m ago" : " minutes ago"
let hourString = isShort ? "h ago" : " hour ago"
let hoursString = isShort ? "h ago" : " hours ago"
let dayString = isShort ? "d ago" : " day ago"
let daysString = isShort ? "d ago" : " days ago"
let weekString = isShort ? "w ago" : " week ago"
let weeksString = isShort ? "w ago" : " weeks ago"
let monthString = isShort ? "mo ago" : " month ago"
let monthsString = isShort ? "mo ago" : " months ago"
let yearString = isShort ? "y ago" : " year ago"
let yearsString = isShort ? "y ago" : " years ago"
if secondsAgo < twoSeconds {
formattedString = "\(secondsAgo)\(secondString)"
} else if secondsAgo < minute {
formattedString = "\(secondsAgo)\(secondsString)"
} else if secondsAgo < twoMinutes {
formattedString = "\(secondsAgo / minute)\(minuteString)"
} else if secondsAgo < hour {
formattedString = "\(secondsAgo / minute)\(minutesString)"
} else if secondsAgo < twoHours {
formattedString = "\(secondsAgo / hour)\(hourString)"
} else if secondsAgo < day {
formattedString = "\(secondsAgo / hour)\(hoursString)"
} else if secondsAgo < twoDays {
formattedString = "\(secondsAgo / day)\(dayString)"
} else if secondsAgo < week {
formattedString = "\(secondsAgo / day)\(daysString)"
} else if secondsAgo < twoWeeks {
formattedString = "\(secondsAgo / week)\(weekString)"
} else if secondsAgo < month {
formattedString = "\(secondsAgo / week)\(weeksString)"
} else if secondsAgo < twoMonths {
formattedString = "\(secondsAgo / month)\(monthString)"
} else if secondsAgo < year {
formattedString = "\(secondsAgo / month)\(monthsString)"
} else if secondsAgo < twoYears {
formattedString = "\(secondsAgo / year)\(yearString)"
} else {
formattedString = "\(secondsAgo / year)\(yearsString)"
}
return formattedString
}
用途:
let formattedTimeAgo = self.getTimeSince(date: date, isShort: false)