How can I compare two dates (NSDate) in Swift 3 and get the days between them?

How to compare two dates in Swift 3? I found many solutions for other versions of Swift, but they do not work for me.

let clickedDay:String = currentcell.DayLabel.text! let currentDate = NSDate() let currentDay = "" //want the current day let dateFormatter = DateFormatter() var dateAsString = "" if Int(clickedDay)! < 10 { dateAsString = "0" + clickedDay + "-11-2016 GMT" } else { dateAsString = clickedDay + "-11-2016 GMT" } dateFormatter.dateFormat = "dd-MM-yyyy zzz" let clickedDate = dateFormatter.date(from: dateAsString) if currentDate as Date >= clickedDate! as Date { return true } else { //Want to get the days between the currentDate and clickedDate let daysLeft = "" } 

I am comparing tow dates, but I need days that are between these tow dates. Can someone help me pls? Thanks and welcome

Added: If I have a date 11/22/2016 and currentdate (now 11/18/2016), I need days between them (in this case 4)

+7
xcode swift swift3 nsdate date-comparison
source share
4 answers

Just make a Date extension like this and get the difference in days.

 extension Date { func daysBetweenDate(toDate: Date) -> Int { let components = Calendar.current.dateComponents([.day], from: self, to: toDate) return components.day ?? 0 } } 

Now use this extension as

 let numOfDays = fromDate.daysBetweenDate(toDate: toDate) 

You can use DateFormatter to convert String to Date like this.

 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" dateFormatter.timeZone = TimeZone(abbreviation: "GMT") let date = dateFormatter.date(from: "18-11-2016") 
+11
source share

Use this function to get all intermediate dates between two dates, just check if the date of the date (final date) is higher than the date (first date), and then the order is +1, otherwise the order is -1: -

 func selectDates(from upDate : Date, to downDate : Date, order : Int){ var selectDate = Calendar.current.date(byAdding: .day, value: order, to: upDate)! while compareDate(dateInitial: selectDate, dateFinal: downDate) != true{ print(" intermediate date is \(selectDate)") selectDate = Calendar.current.date(byAdding: .day, value: order, to: selectDate)! } print("*** intermediate dates are selected ***") } func compareDate(dateInitial:Date, dateFinal:Date) -> Bool { let order = Calendar.current.compare(dateInitial, to: dateFinal, toGranularity: .day) switch order { case .orderedSame: return true default: return false } } 
+2
source share

Try this one

 let startDateComparisionResult:ComparisonResult = currentDate.compare(clickedDate! as NSDate) if startDateComparisionResult == ComparisonResult.orderedAscending { // Current date is smaller than end date. } else if startDateComparisionResult == ComparisonResult.orderedDescending { // Current date is greater than end date. } else if startDateComparisionResult == ComparisonResult.orderedSame { // Current date and end date are same } 
+2
source share

Try:

 let days=Calendar.current.dateComponents([.day], from: date_1, to: date_2) let nb_days=days.day 
+1
source share

All Articles