Check if date is before current date (Swift)

Tom Coomer picture Tom Coomer · Nov 7, 2014 · Viewed 60.8k times · Source

I would like to check if a NSDate is before (in the past) by comparing it to the current date. How would I do this?

Thanks

Answer

Kibo picture Kibo · Jan 26, 2016

I find the earlierDate method.

if date1.earlierDate(date2).isEqualToDate(date1)  {
     print("date1 is earlier than date2")
}

You also have the laterDate method.

Swift 3 to swift 5:

if date1 < date2  {
     print("date1 is earlier than date2")
}