Compare two dates using swift in ios

 
Compare two dates in swift

During iOS app development, sometimes we need to compare two dates and then check out the difference whether
1) both dates are equal 
2) date is smaller than the current date(or particular date we are comparing with)
3) date is greater then other date(or particular date we are comparing with)

In iOS sdk, Apple provide compare function to compare two different date objects or two different dates. In this tutorial i will share the code that shows how to compare two Dates using swift in iOS app development.

For this tutorial,  we will create two different date objects using DateFormatter. Then we will, use compare function of Date class to check whether our two different date objects are same, or one date is greater than other date and lastly one date is smaller than other date. We will use iOS swift language for this tutorial.

Different cases while we compare two dates

Same dates: Both dates are equal

Smaller first date: First date is smaller then the second date

Bigger first date : First date is greater then the second date

Final notes:
In this tutorial, you learned how to check if
1) two dates are same,
2) one date is greater than other date and
3) one date is smaller than other date using swift in iOS.

If you have any queries or suggestion, please feel free to comment.

Articles you are interested in