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

前端 未结 19 2039
时光说笑
时光说笑 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:53

    This is the shorter version: Basically I try to get the difference between the post timestamp with the Date() now.

    // MARK: - UPDATE Time Stamp
    static func updateTimeStampPost(postTimeStamp: Date?, _ completion: (_ finalString: String?) -> Void) {
        // date in the current state
        let date = Date()
        let dateComponentFormatter = DateComponentsFormatter()
    
        // change the styling date, wether second minute or hour
        dateComponentFormatter.unitsStyle = .abbreviated
        dateComponentFormatter.allowedUnits = [.second, .minute, .hour, .day, .weekOfMonth]
        dateComponentFormatter.maximumUnitCount = 1
    
        // return the date new format as a string in the completion
        completion(dateComponentFormatter.string(from: postTimeStamp!, to: date))
    }
    

提交回复
热议问题