Get number of days between two dates in swift

Get number of days between two dates in siwft

Want to calculate number of days between two dates in swift, or difference of days between two dates in swift.

If you are looking for objective C tutorial for getting number of days between two dates in Objective C, then check given below link

Getting number of days between two dates

Below is the code snippet, written in swift that will return number of days between two dates.

//MARK:- daysBetween
    func daysBetween(start: Date, end: Date) -> Int {
        return Calendar.current.dateComponents([.day], from: start, to: end).day!
    }

In above code, we will pass two date objects. Start date and end date. Function will return us the count of days between the two dates as integer. Inside our function named daysBetween, we will use Calendar class dateComponents function that return only day component of the date. Because a date is a collection of different components, like day, month, year etc.

Where to go from here

In this tutorial, we learned how to calculate count of days between two different dates in iOS app development using swift language.