Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift

前端 未结 19 2038
时光说笑
时光说笑 2020-11-22 02:16

I am trying to get the difference between the current date as NSDate() and a date from a PHP time(); call for example: NSDate(timeIntervalSin

19条回答
  •  孤独总比滥情好
    2020-11-22 02:57

    Swift 5.1 • iOS 13

    You can use RelativeDateFormatter that has been introduced by Apple in iOS 13.

    let exampleDate = Date().addingTimeInterval(-15000)
    
    let formatter = RelativeDateTimeFormatter()
    formatter.unitsStyle = .full
    let relativeDate = formatter.localizedString(for: exampleDate, relativeTo: Date())
    
    print(relativeDate) // 4 hours ago
    

    See How to show a relative date and time using RelativeDateTimeFormatter.

提交回复
热议问题