Swift if Given date is before Current Date with example

In these tutorials, shows if given date is before current date or today.

The below examples check only Dates, not time

Check if given date is before today date in swift?

Current date returns using new Date() object. You can create past and future date using date method It accepts

byAdding: tells adding .day or any value: value can be positive or negative to: specify current date.

Dates can be compared using < operator

Here is an example to check give date is before current date

import Foundation
let date = Date()
let pastDate = Calendar.current.date(byAdding: .day, value: -1, to: date)!
print(date) //2022-07-03 06:13:45 +0000
print(pastDate)//2022-10-30 06:13:45 +0000


if(pastDate<date){
  print("pastDate is before current date")
}

Output:

2022-07-03 06:13:45 +0000
2022-07-04 06:13:45 +0000
2022-10-30 06:13:45 +0000
pastDate is before current date

Check if given date is after today date in swift?

Dates can be compared using > for checking after date.

import Foundation
let date = Date()
let futureDate = Calendar.current.date(byAdding: .day, value: 1, to: date)!
print(date) //2022-07-03 06:13:45 +0000
print(futureDate)//2022-07-04 06:13:45 +0000

if(futureDate>date){
  print("futureDate is before current date")
}

Output:

2022-07-03 06:13:45 +0000
2022-07-04 06:13:45 +0000
futureDate is before current date