mercredi 5 août 2020

How to get the days went by between two dates in Swift IOS? [duplicate]

I need to get the days went by between to dates in Swift. When we take 05.08.2020 and 10.08.2020 as an example it is fairly easy because the result would be 5. I tried the following and wanted to compare the Int values of Year, month and day but it turned out that this is going to be extremely complicated as different months have different amount of days.

    let currentDate = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "dd.MM.yyyy"
    
    // date the user registered: registrationDate of type String
    let regDay = Int(registrationDate[0 ..< 2])
    let regMonth = Int(registrationDate[3 ..< 5])
    let regYear = Int(registrationDate[6 ..< 10])
    
    // current date 
    let currentDay = Int(formatter.string(from: currentDate)[0 ..< 2])
    let currentMonth = Int(formatter.string(from: currentDate)[3 ..< 5])
    let currentYear = Int(formatter.string(from: currentDate)[6 ..< 10])

I appreciate any kind of help with this problem. If it helps, for now, I only need the amount of days went by for the year 2020.

Aucun commentaire:

Enregistrer un commentaire