doesRelativeDateFormatting 自定义样式 - 这可能吗?
doesRelativeDateFormatting with a custom style - is it possible?
我想将 doesRelativeDateFormatting
与 Swift 结合使用,以便在我的应用程序上显示日期时获得更多人类可读的日期,例如 "today" 或 "tomorrow" .但是,当显示非相对日期时,我想显示自定义样式,例如 "Wed, Feb 10 '18"。
到目前为止,我可以将预定义的 dateStyle
之一与我的 DateFormatter 对象一起使用,例如 .short
或 .medium
,但其中的 none 显示缩写工作日和月份。当使用字符串中的自定义格式时,例如 "EEE, MMM d yy",我会丢失相对日期。
这是一种同时使用两者并显示相关日期(当它们存在时)并为所有其他日期自定义日期的方法吗?
当不使用相对格式时,没有直接的方法来获取相对格式和自定义格式。充其量您可以指定样式,但不能指定格式。
一种解决方案是使用一种使用三个日期格式化程序的辅助方法。一种使用具有所需样式的相对格式,一种不是相对但使用相同样式,另一种使用您的非相对日期自定义格式。
func formatDate(_ date: Date) -> String {
// Setup the relative formatter
let relDF = DateFormatter()
relDF.doesRelativeDateFormatting = true
relDF.dateStyle = .long
relDF.timeStyle = .medium
// Setup the non-relative formatter
let absDF = DateFormatter()
absDF.dateStyle = .long
absDF.timeStyle = .medium
// Get the result of both formatters
let rel = relDF.string(from: date)
let abs = absDF.string(from: date)
// If the results are the same then it isn't a relative date.
// Use your custom formatter. If different, return the relative result.
if (rel == abs) {
let fullDF = DateFormatter()
fullDF.setLocalizedDateFormatFromTemplate("EEE, MMM d yy")
return fullDF.string(from: date)
} else {
return rel
}
}
print(formatDate(Date()))
print(formatDate(Calendar.current.date(byAdding: .day, value: 1, to: Date())!))
print(formatDate(Calendar.current.date(byAdding: .day, value: 7, to: Date())!))
输出:
Today at 11:01:16 AM
Tomorrow at 11:01:16 AM
Tue, Feb 20, 18
如果您需要格式化大量日期,您需要修改此代码,以便它一次性创建所有格式化程序,然后在此方法中重复使用它们。
我想将 doesRelativeDateFormatting
与 Swift 结合使用,以便在我的应用程序上显示日期时获得更多人类可读的日期,例如 "today" 或 "tomorrow" .但是,当显示非相对日期时,我想显示自定义样式,例如 "Wed, Feb 10 '18"。
到目前为止,我可以将预定义的 dateStyle
之一与我的 DateFormatter 对象一起使用,例如 .short
或 .medium
,但其中的 none 显示缩写工作日和月份。当使用字符串中的自定义格式时,例如 "EEE, MMM d yy",我会丢失相对日期。
这是一种同时使用两者并显示相关日期(当它们存在时)并为所有其他日期自定义日期的方法吗?
当不使用相对格式时,没有直接的方法来获取相对格式和自定义格式。充其量您可以指定样式,但不能指定格式。
一种解决方案是使用一种使用三个日期格式化程序的辅助方法。一种使用具有所需样式的相对格式,一种不是相对但使用相同样式,另一种使用您的非相对日期自定义格式。
func formatDate(_ date: Date) -> String {
// Setup the relative formatter
let relDF = DateFormatter()
relDF.doesRelativeDateFormatting = true
relDF.dateStyle = .long
relDF.timeStyle = .medium
// Setup the non-relative formatter
let absDF = DateFormatter()
absDF.dateStyle = .long
absDF.timeStyle = .medium
// Get the result of both formatters
let rel = relDF.string(from: date)
let abs = absDF.string(from: date)
// If the results are the same then it isn't a relative date.
// Use your custom formatter. If different, return the relative result.
if (rel == abs) {
let fullDF = DateFormatter()
fullDF.setLocalizedDateFormatFromTemplate("EEE, MMM d yy")
return fullDF.string(from: date)
} else {
return rel
}
}
print(formatDate(Date()))
print(formatDate(Calendar.current.date(byAdding: .day, value: 1, to: Date())!))
print(formatDate(Calendar.current.date(byAdding: .day, value: 7, to: Date())!))
输出:
Today at 11:01:16 AM
Tomorrow at 11:01:16 AM
Tue, Feb 20, 18
如果您需要格式化大量日期,您需要修改此代码,以便它一次性创建所有格式化程序,然后在此方法中重复使用它们。