8

I have two dates and I want to compare it. How can I compare dates? I have to date objects. Say modificateionDate older updatedDate.

So which is the best practice to compare dates?

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
Ashwin Kanjariya
  • 1,019
  • 2
  • 10
  • 22

4 Answers4

11

Date now conforms to Comparable protocol. So you can simply use <, > and == to compare two Date type objects.

if modificateionDate < updatedDate {
     //modificateionDate is less than updatedDate
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 1
    @AshwinKanjariya You have told you are having to `Date` instance then this will work, I think you are having `NSDate` instance confirm this first here show your code. Batter if you use `Date` instead of `NSDate` in Swift 3 – Nirav D Feb 17 '17 at 10:21
  • 1
    @AshwinIndianic Welcome mate :) – Nirav D Feb 17 '17 at 12:14
  • @AshwinIndianic Now I'm know why you are not accepting my answer because you are working with 2 accounts and want to accept answer from that answer. I have checked your question http://stackoverflow.com/questions/42336935/archive-is-not-success-due-to-error-code-1-although-i-can-do-build-and-instal The way you are working is not good, soon you will be know that. – Nirav D Feb 21 '17 at 08:51
  • @AshwinIndianic So now you have changed the username so no one will notice this, Nice try keep going :) – Nirav D Feb 21 '17 at 10:34
7

Per @NiravD's answer, Date is Comparable. However, if you want to compare to a given granularity, you can use Calendar's compare(_:to:toGranularity:)

Example…

let dateRangeStart = Date()
let dateRangeEnd = Date().addingTimeInterval(1234)

// Using granularity of .minute
let order = Calendar.current.compare(dateRangeStart, to: dateRangeEnd, toGranularity: .minute)

switch order {
case .orderedAscending:
    print("\(dateRangeEnd) is after \(dateRangeStart)")
case .orderedDescending:
    print("\(dateRangeEnd) is before \(dateRangeStart)")
default:
    print("\(dateRangeEnd) is the same as \(dateRangeStart)")
}

> 2017-02-17 10:35:48 +0000 is after 2017-02-17 10:15:14 +0000

// Using granularity .hour
let order = Calendar.current.compare(dateRangeStart, to: dateRangeEnd, toGranularity: .hour)

> 2017-02-17 10:37:23 +0000 is the same as 2017-02-17 10:16:49 +0000
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
3

Swift iOS 8 and up When you need more than simply bigger or smaller date comparisons. For example is it the same day or the previous day,...

Note: Never forget the timezone. Calendar timezone has a default, but if you do not like the default, you have to set the timezone yourself. To know which day it is, you need to know in which timezone you are asking.

extension Date {
    func compareTo(date: Date, toGranularity: Calendar.Component ) -> ComparisonResult  {
        var cal = Calendar.current
        cal.timeZone = TimeZone(identifier: "Europe/Paris")!
        return cal.compare(self, to: date, toGranularity: toGranularity)
        }
    }

Use it like this:

if thisDate.compareTo(date: Date(), toGranularity: .day) == .orderedDescending {
// thisDate is a previous day
}

For a more complex example how to use this in a filter see this:

https://stackoverflow.com/a/45746206/4946476

t1ser
  • 1,574
  • 19
  • 18
-1

Swift has ComparisonResult with orderedAscending, orderedDescending and same as below.

if modificateionDate.compare(updatedDate) == ComparisonResult.orderedAscending {
            //Do what you want
}

Hope this may help you.

Kanjariya-IOS
  • 652
  • 1
  • 5
  • 17
  • 1
    `Swift has ComparisonResult with orderedAscending` No. This is from Foundation. Swift itself does not have these. – Eric Aya Feb 17 '17 at 11:51
  • Now i'm 100 % sure that these both a/c is handler by you @AshwinIndianic. You think that you changed the name for both user and you survived here :P :D Nice going :- – Nirav D Feb 23 '17 at 09:04
  • Thank you for noticing but what you think is not correct...You can ding for this in detail I don't mind. – Kanjariya-IOS Feb 23 '17 at 09:09
  • @Kanjariya-IOS What I notice is correct and that you and me both know :P If both of you are different person then my above answer should be accepted by "@Ashwin Indianic" because he is accepting your answer in the other question. :- – Nirav D Feb 23 '17 at 09:25