How can i compare two dates (NSDate) in Swift 3 and get the days between them?
13,194
Solution 1
Simply make extension of Date
like this and get 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 like this
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")
Solution 2
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
}
Solution 3
Try:
let days=Calendar.current.dateComponents([.day], from: date_1, to: date_2)
let nb_days=days.day
Solution 4
Use this function to get all intermediate dates between two dates, just check if down date (final date) is greater than up date (first date) then order is +1, else order -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
}
}
Author by
moxmlb
Updated on July 29, 2022Comments
-
moxmlb almost 2 years
How can I compare two dates in Swift 3? I found many solutions for other Swift versions, but they doesn't 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 compare the tow dates, but i want the days which are between that tow dates. Can someone help me pls? Thanks and greets
Added: If I have the date 22.11.2016 and the currentdate (now 18.11.2016) i want the days between that (in this case 4)
-
moxmlb over 7 yearsI want the days, how long it is to the next date
-
moxmlb over 7 yearsI want the days, how long it is to the next date
-
Sapana Ranipa over 7 yearsDepends on your currentDate and clickedDate value
-
Marie Dm over 7 yearsI'm not sure I understand you well. My answer gives you the number of days between your_date and Date() (so today). But if you want to compare with another date just change Date() with next_date.
-
moxmlb over 7 yearsYes thats what i want. but in days I have no number, how get I the number?
-
moxmlb over 7 yearsWhere i declaire the extension?
-
moxmlb over 7 yearsGet this error: use of unresolved identifier 'fromDate'
-
moxmlb over 7 yearsThat works, but if i have the 18.11.2016 (fromdate) and 22.11.2016 (todate), it says 3 days
-
moxmlb over 7 yearsHow i give them the right timezone? like this: dateAsString = clickedDay + "-11-2016 GMT" ??
-
Nirav D over 7 years@Mbach Check the edited answer to convert string to date, there is no need to add "GMT" with your dateString.
-
Nirav D over 7 years@Mbach I'm talking about this
"0" + clickedDay + "-11-2016 GMT"
-
Nirav D over 7 years@Mbach Welcome :)
-
Marie Dm over 7 yearsSorry I forgot the second part :) I updated my answer @Mbach