- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare two NSDates in iPhone/iOS?
In this article we'll see how to compare two NSDates in swift. First of all we'll need to create two NSDates.
We'll do it in playground instead of simulator this time.
First Let's create two different dates.
let dateOne = NSDateComponents() dateOne.day = 5 dateOne.month = 6 dateOne.year = 1993 let dateTwo = NSDateComponents() dateTwo.day = 4 dateTwo.month = 2 dateTwo.year = 1995
Using these date components we'll create dates and then compare them
let cal = NSCalendar.current let FirstDate = cal.date(from: dateOne as DateComponents) let secondDate = cal.date(from: dateTwo as DateComponents)
Now to compare them we'll use a if condition.
if secondDate!.compare(firstDate!) == .orderedAscending { print("date 1 is bigger than date 2") } else { print("Date 2 is bigger") }
Below is the output of above code when running on simulator.
there are three different ways we can compare.
- orderedAscending
- orderedDescending
- orderedSame
- Related Articles
- How to parse JSON object in iPhone/iOS?
- How to create a WebView in iOS/iPhone?
- How to take a screenshot programmatically in iPhone/iOS?
- How to access RESTFul services from iOS/iPhone?
- How to add line break for UILabel in iOS/iPhone?
- How to add 1 day to a Date in iOS/iPhone?
- How to request permission programatically to use location services in iPhone/iOS?
- How to get the MAC address of an iOS/iPhone programmatically?
- How to use Bold & Non-Bold Text In A Single UILabel in iOS/iPhone?
- How do I draw a shadow under a UIView in iPhone/iOS?
- How to create a Border, Border radius, and shadow to a UIView in iPhone/iOS?
- How to go through all text fields with the "Next" Button on the iPhone/iOS Keyboard?
- How to compare two dates in Java?
- How to compare two numbers in JavaScript?
- How to Compare two String in Java?

Advertisements