我有一个createdAt时间戳。该createdAt时间戳将与当前日期(Date()
)进行比较。
假设当前日期为2018年8月1日。如果createdAt是2018年7月28日,则仅显示2018年7月28日。但是,如果createdAt日期是同一天(2018年8月1日),则仅显示2 m之前,5h 38m 15年前,像这样的东西:
我尝试使用下面的代码,但结果不一致。如果日期不同,但下面的代码仍显示5h 16m。
func calculateTimeDifference() -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
guard let date1 = dateFormatter.date(from: dateTimeString) else {
return nil
}
let date2 = Date()
let components : Set<Calendar.Component> = [.second, .minute, .hour, .day, .month, .year]
let difference = Calendar.current.dateComponents(components, from: date1, to: date2)
var dateTimeDifferenceString = ""
var secondDifference = ""
var minuteDifference = ""
var hourDifference = ""
var dayDifference = ""
if difference.second! < 0 {
secondDifference = "0"
} else {
secondDifference = String(difference.second!)
}
if difference.minute! < 0 {
minuteDifference = "0"
} else {
minuteDifference = String(difference.minute!)
}
if difference.hour! < 0 {
hourDifference = "0"
} else {
hourDifference = String(difference.hour!)
}
if difference.day! < 0 {
dayDifference = "0"
} else{
dayDifference = String(difference.day!)
}
if difference.minute! == 0 {
dateTimeDifferenceString = "\(secondDifference)s"
} else if difference.hour == 0 {
dateTimeDifferenceString = "\(minuteDifference)m"
} else if difference.day == 0 {
dateTimeDifferenceString = "\(hourDifference)h \(minuteDifference)m"
} else if difference.day! > 0 {
dateTimeDifferenceString = "different day \(dayDifference)"
}
return dateTimeDifferenceString
}
可以请您分享您的代码吗?
答案 0 :(得分:1)
我个人会使用DateComponentsFormatter
,这是一个比较安全的选择,然后尝试按时执行数学运算,这绝对不是一个好主意
let duration = Date().timeIntervalSince(date1)
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .abbreviated
formatter.allowedUnits = [ .day, .hour, .minute, .second]
let durationFormat = formatter.string(from: duration)
有关更多详细信息,请查看Formatting a Duration with NSDateComponentsFormatter。
答案 1 :(得分:0)
我认为,第一个问题在这里:
let components : Set<Calendar.Component> = [.second, .minute, .hour, .day, .month, .year]
您最大的信息是天,但是直到年您都可以获得组件。结果将是:2018-07-01 to 2018-08-01 => year=0, month=1, day=0
如果最多只需要Day组件,则将组件设置为Maximum Day,如下所示。
let components : Set<Calendar.Component> = [.second, .minute, .hour, .day]
结果将为:2018-07-01 to 2018-08-01 => year=nil, month=nil, day=31
第二个问题是您确定dateTimeDifferenceString
的逻辑。我认为应该是这样。
func calculateTimeDifference() -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
guard let date1 = dateFormatter.date(from: dateTimeString) else {
return nil
}
let date2 = Date()
let components : Set<Calendar.Component> = [.second, .minute, .hour, .day]
let difference = Calendar.current.dateComponents(components, from: date1, to: date2)
var dateTimeDifferenceString = ""
if difference.day! > 0 {
dateTimeDifferenceString = "different day \(difference.day!)"
} else if difference.hour! > 0 {
dateTimeDifferenceString = "\(difference.hour!)h \(difference.minute!)m"
} else if difference.minute! > 0 {
dateTimeDifferenceString = "\(difference.minute!)m"
} else {
dateTimeDifferenceString = "\(difference.second!)s"
}
return dateTimeDifferenceString
}