如何显示与当前日期相比的时差?
Posted
技术标签:
【中文标题】如何显示与当前日期相比的时差?【英文标题】:How to show time day difference compared to current date? 【发布时间】:2018-08-01 02:38:05 【问题描述】:我有一个 createdAt 时间戳。 createdAt 时间戳将与当前日期 (Date()
) 进行比较。
假设 currentDate 是 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
你能分享你的代码吗?
【问题讨论】:
【参考方案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了解更多详情。
【讨论】:
【参考方案2】:我认为,第一个问题在这里:
let components : Set<Calendar.Component> = [.second, .minute, .hour, .day, .month, .year]
您的最大信息是 Day,但您会在 Year 之前获得组件。这将导致:2018-07-01 to 2018-08-01 => year=0, month=1, day=0
如果您最多只需要 Day 组件,请将组件设置为最大 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
【讨论】:
以上是关于如何显示与当前日期相比的时差?的主要内容,如果未能解决你的问题,请参考以下文章
如何显示日期来自jQuery instated显示当前日期?