Compare two dates using swift in ios

 
Compare two dates in swift

In so many of our app development, we need to compare two dates and then check whether both dates are equal or either small or greater then other date. In iOS, 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.

Here we will create two different date objects using DateFormatter. Then use compare function of Date class to check whether our two different date objects are same,one date is greater than other date or one date is smaller than other date in swift.

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

%d bloggers like this: