doesRelativeDateFormatting 不适用于 swift 中的未来日期
doesRelativeDateFormatting not working for future date in swift
/// A date formatter for displaying the complete relative date plus the time plus day, no seconds.
///
/// Examples: Today at 2:07 PM; Yesterday at 2:07 PM; Monday, Oct 7, 2018 at 5:05 AM
///
static let relativeFullDateShortFormatterWithDay: DateFormatter = {
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .medium
formatter.doesRelativeDateFormatting = true
formatter.timeZone = TimeZone.current
formatter.locale = Locale.autoupdatingCurrent
return formatter
}()
我正在从服务器收到到期日期。如果到期日是今天,我想显示今天,如果到期日在一周之内,我想显示工作日名称。
为此,我将 doesRelativeDateFormatting 设置为 true。我从服务器上得到的到期日期是 9 月 4 日,也就是星期五。当我使用 relativeFullDateShortFormatterWithDay 将 Date 转换为字符串时,它返回 Sep 4, 2020 而不是 Friday。
我需要在格式化程序中设置更多设置吗?对于过去的日期,这工作正常。
给定问题中的格式化程序和一个特殊的工作日格式化程序
let weekDayFormatter = DateFormatter()
weekDayFormatter.dateFormat = "EEEE HH:mm"
(如果语言环境始终相同,则格式可以更改为英语的“EEEE 'at' HH:mm”)
然后我们可以使用格式化日期的函数,首先检查第一个格式化程序 returns 是否类似于“今天”,然后 returns 然后检查给定日期是否在下周然后 returns 一个工作日
func format(date: Date) -> String {
formatter.doesRelativeDateFormatting = true
let relative = formatter.string(from: date)
formatter.doesRelativeDateFormatting = false
let absolute = formatter.string(from: date)
if relative != absolute {
return relative
}
let calendar = Calendar.current
let startDate = calendar.startOfDay(for: Date())
guard let endDate = calendar.date(byAdding: .day, value: 7, to: startDate) else {
return formatter.string(from: date)
}
if date > startDate && date < endDate {
return weekDayFormatter.string(from: date)
} else {
return formatter.string(from: date)
}
}
例子
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 1)))
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 4)))
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 12)))
Tomorrow at 17:07
Saturday 17:07
13 Sep 2020 at 17:07
/// A date formatter for displaying the complete relative date plus the time plus day, no seconds.
///
/// Examples: Today at 2:07 PM; Yesterday at 2:07 PM; Monday, Oct 7, 2018 at 5:05 AM
///
static let relativeFullDateShortFormatterWithDay: DateFormatter = {
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .medium
formatter.doesRelativeDateFormatting = true
formatter.timeZone = TimeZone.current
formatter.locale = Locale.autoupdatingCurrent
return formatter
}()
我正在从服务器收到到期日期。如果到期日是今天,我想显示今天,如果到期日在一周之内,我想显示工作日名称。 为此,我将 doesRelativeDateFormatting 设置为 true。我从服务器上得到的到期日期是 9 月 4 日,也就是星期五。当我使用 relativeFullDateShortFormatterWithDay 将 Date 转换为字符串时,它返回 Sep 4, 2020 而不是 Friday。 我需要在格式化程序中设置更多设置吗?对于过去的日期,这工作正常。
给定问题中的格式化程序和一个特殊的工作日格式化程序
let weekDayFormatter = DateFormatter()
weekDayFormatter.dateFormat = "EEEE HH:mm"
(如果语言环境始终相同,则格式可以更改为英语的“EEEE 'at' HH:mm”)
然后我们可以使用格式化日期的函数,首先检查第一个格式化程序 returns 是否类似于“今天”,然后 returns 然后检查给定日期是否在下周然后 returns 一个工作日
func format(date: Date) -> String {
formatter.doesRelativeDateFormatting = true
let relative = formatter.string(from: date)
formatter.doesRelativeDateFormatting = false
let absolute = formatter.string(from: date)
if relative != absolute {
return relative
}
let calendar = Calendar.current
let startDate = calendar.startOfDay(for: Date())
guard let endDate = calendar.date(byAdding: .day, value: 7, to: startDate) else {
return formatter.string(from: date)
}
if date > startDate && date < endDate {
return weekDayFormatter.string(from: date)
} else {
return formatter.string(from: date)
}
}
例子
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 1)))
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 4)))
print(format(date: Date(timeIntervalSinceNow: 60 * 60 * 24 * 12)))
Tomorrow at 17:07
Saturday 17:07
13 Sep 2020 at 17:07