1. 今天要做这个效果(在时间旁边显示今天,昨天)
2. Preview
3. Code:
// 添加 今天 昨天
func show_today_yesterday(d: Date = Date()) -> String {
let calendar = Calendar.current
let today: Date = Date()
if calendar.isDate(today, inSameDayAs: d) {
return " · 今天"
} else {
if let interval = calendar.dateComponents([.day], from: d, to: today).day {
if interval == 1 {
return " · 昨天"
} else {
return ""
}
} else {
print("日期计算出错")
return ""
}
}
}