Search code examples
iosswiftxcodecocoa-touchnsdate

Check if date is before current date (Swift)


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


Solution

  • 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")
    }