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

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

    Some addition in jose920405 answer to make it compatible with Swift 3.0 and above

    func getDateTimeDiff(dateStr:String) -> String {
        
        let formatter : DateFormatter = DateFormatter()
        formatter.timeZone = NSTimeZone.local
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        let now = formatter.string(from: NSDate() as Date)
        let startDate = formatter.date(from: dateStr)
        let endDate = formatter.date(from: now)
        
        // *** create calendar object ***
        var calendar = NSCalendar.current
        
        // *** Get components using current Local & Timezone ***
        print(calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: startDate!))
        
        // *** define calendar components to use as well Timezone to UTC ***
        let unitFlags = Set([.year, .month, .day, .hour, .minute, .second])
        calendar.timeZone = TimeZone(identifier: "UTC")!
        let dateComponents = calendar.dateComponents(unitFlags, from: startDate!, to: endDate!)
        
        // *** Get Individual components from date ***
        let years = dateComponents.year!
        let months = dateComponents.month!
        let days = dateComponents.day!
        let hours = dateComponents.hour!
        let minutes = dateComponents.minute!
        let seconds = dateComponents.second!
        
        var timeAgo = ""
        
        if (seconds > 0){
            if seconds < 2 {
                timeAgo = "Second Ago"
            }
            else{
                timeAgo = "\(seconds) Second Ago"
            }
        }
        
        if (minutes > 0){
            if minutes < 2 {
                timeAgo = "Minute Ago"
            }
            else{
                timeAgo = "\(minutes) Minutes Ago"
            }
        }
        
        if(hours > 0){
            if hours < 2 {
                timeAgo = "Hour Ago"
            }
            else{
                timeAgo = "\(hours) Hours Ago"
            }
        }
        
        if (days > 0) {
            if days < 2 {
                timeAgo = "Day Ago"
            }
            else{
                timeAgo = "\(days) Days Ago"
            }
        }
        
        if(months > 0){
            if months < 2 {
                timeAgo = "Month Ago"
            }
            else{
                timeAgo = "\(months) Months Ago"
            }
        }
        
        if(years > 0){
            if years < 2 {
                timeAgo = "Year Ago"
            }
            else{
                timeAgo = "\(years) Years Ago"
            }
        }
        
        DLog("timeAgo is ===> \(timeAgo)")
        return timeAgo;
    }
    

提交回复
热议问题