Swift iOS doesRelativeDateFormatting have different values besides “Today” and “Yesterday”?

后端 未结 4 1823
长发绾君心
长发绾君心 2021-02-12 21:17

I have a number of dates that I am trying to represent using a relative date such as \"Today, Yesterday, 1 week ago, 1 month ago\" etc...

This is the Swift code I am usi

4条回答
  •  盖世英雄少女心
    2021-02-12 21:26

    iOS 13+ now has RelativeDateFormatter

    For examples: https://nshipster.com/formatter/#relativedatetimeformatter

    let formatter = RelativeDateTimeFormatter()
    formatter.localizedString(from: DateComponents(day: 1, hour: 1)) // "in 1 day"
    formatter.localizedString(from: DateComponents(day: -1)) // "1 day ago"
    

    or

    formatter.localizedString(for: date, relativeTo: Date())
    

提交回复
热议问题