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

后端 未结 4 1825
长发绾君心
长发绾君心 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:34

    You should have a look at Mattt Thompsons Blog post about NSDateComponentsFormatter

    http://nshipster.com/nsdatecomponents/ and http://nshipster.com/nsformatter/

    let formatter = NSDateComponentsFormatter()
    formatter.unitsStyle = .Full
    
    let components = NSDateComponents()
    components.day = 1
    components.hour = 2
    
    let string = formatter.stringFromDateComponents(components)
    // 1 day, 2 hours
    

    Example (by Mattt Thompson)

提交回复
热议问题