Muhammad Iqbal
Muhammad Iqbal

Reputation: 119

Comparing two dates for getting components

I am comparing two dates and it works perfectly when we perform the operation on the same month however when month is higher i.e aug 8 and date is lower let say 2 and current date is 27 and mont is July 7 so for this comparison I am getting wrong and negative values.

Here is my function:

func countDonwn(isStumpStarted: Bool = false)-> String? {
    let date = Date()
    
    let calendar = Calendar.current
    let components = calendar.dateComponents([.hour, .minute, .second, .day], from: date as Date)
    let currentDate = calendar.date(from: components)?.toLocalTime()
    let userCalendar = Calendar.current
    guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")) else {return nil }
    let competitionDate = calendar.dateComponents([.hour, .minute, .second, .day], from: endDte as Date)
    if endDte < (date as Date) {
        return ""
    }
    
    let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!.toLocalTime()
    let CompetitionDayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: competitionDay)
    let daysLeft = CompetitionDayDifference.day
    let hoursLeft = CompetitionDayDifference.hour
    let minutesLeft = CompetitionDayDifference.minute
    let secondLeft = CompetitionDayDifference.second
    if daysLeft != 0 && !isStumpStarted {
        return "\(String(describing: daysLeft ?? 0))d \(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
        
    } else if daysLeft == 0 && hoursLeft == 0 && minutesLeft == 0 && secondLeft == 0 {
        return ""
    }
    
    return "\(String(format: "%02d", hoursLeft ?? 0)):\(String(format: "%02d", minutesLeft ?? 0)):\(String(format: "%02d", secondLeft ?? 0))"
}

Upvotes: 0

Views: 68

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 52013

The date comparison can be performed using a Calendar function and DateComponents

let calendar = Calendar.current
let delta = calendar.dateComponents([.hour, .minute, .second], 
                                    from: date, 
                                    to: endDte)

print("\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))")

So I think your function can be shortened to

func countDonwn(isStumpStarted: Bool = false)-> String? {
    let date = Date()

    guard let endDte = (self.getDate(defaultFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")),
    date.compare(endDte) == .orderedAscending else {
        return nil
    }

    let calendar = Calendar.current
    let delta = calendar.dateComponents([.hour, .minute, .second], from: fromDate, to: toDate)

    return "\(String(format: "%02d", delta.hour!)):\(String(format: "%02d", delta.minute!)):\(String(format: "%02d", delta.second!))"
}

Note that is ok to force unwrap date components for delta that has been selected when calling the calendar.dateComponents(_ ,from:, to:) function

Upvotes: 1

Related Questions