
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to get the differences between two dates in iOS?
Getting difference between two dates is easy. You should know how to play between the dates.
We will be using DateFormatter class for formatting the dates.
Instances of DateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects.
You can read more about it here
https://developer.apple.com/documentation/foundation/dateformatter
We will also be using Calendar structure, apple has provided beautiful documentation of it,
https://developer.apple.com/documentation/foundation/calendar
So let’s get started.
Open Xcode, New Playground.
Copy the below code
import UIKit // create object of DateFormatter and Calendar let formatter = DateFormatter() let calendar = Calendar.current // specify the format, formatter.dateFormat = "dd-MM-yyyy" // specify the start date let startDate = formatter.date(from: "10-08-2018") // specify the end date let endDate = formatter.date(from: "23-09-2019") print(startDate!) print(endDate!) let diff = calendar.dateComponents([.day], from: startDate!, to: endDate!) // print the diff between the two dates print(diff)
- Related Articles
- How to get the differences between two dates in Android using Kotlin?
- How to get the difference between two dates in Android?
- How to get the number of seconds between two Dates in JavaScript?
- How to get the number of days between two Dates in JavaScript?
- How to get number of quarters between two dates in Java
- C# Program to get the difference between two dates
- How to get the distance between two geographic locations in iOS using Swift?
- C# Program to get the difference between two dates in seconds
- Not able to get the difference between two dates (SAP)
- How to list all dates between two dates in Excel?
- How do I get the number of days between two dates in JavaScript?
- Get the SUM of records between two given dates in MySQL
- How to query between two dates in MySQL?
- How to create a vector with dates between two dates in R?
- How to calculate the difference between two dates in JavaScript?

Advertisements