Calculate the duration between ios dates in the format Years, months and date

Im new with fast programming and I havent been able to find the code to find the difference between the two dates in terms of years, months and days. I tried the following code but it did not work

let form = NSDateComponentsFormatter() form.maximumUnitCount = 2 form.unitsStyle = .Full let s = form.stringFromTimeInterval( date2.timeIntervalSinceReferenceDate - date1.timeIntervalSinceReferenceDate) 

entrance

Date1 = "12 / March / 2015"

Date2 = "1 / June / 2015"

Yield: x years y months z days

I ask for advice

+7
source share
6 answers

If you need the difference (in years, months, days) numerically, then calculate NSDateComponents as in Quick Days between two NSDates or Rajan's answer .

If you need a difference in the quality of the (localized) string to present it to the user, then use NSDateComponentsFormatter , like this

 let form = NSDateComponentsFormatter() form.maximumUnitCount = 2 form.unitsStyle = .Full form.allowedUnits = [.Year, .Month, .Day] let s = form.stringFromDate(date1, toDate: date2) 

As mentioned in the comments, calculating the difference from the net time interval between dates cannot give the correct one because most of the date information is lost.

Update for Swift 3:

 let form = DateComponentsFormatter() form.maximumUnitCount = 2 form.unitsStyle = .full form.allowedUnits = [.year, .month, .day] let s = form.string(from: date1, to: date2) 
+6
source

We can use this function in Swift 2.0.

 func yearsBetweenDate(startDate: NSDate, endDate: NSDate) -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Year], fromDate: startDate, toDate: endDate, options: []) return components.year } 

You can return everything that I returned a year in this function. This will return the number of years between two dates.
You can simply write months, days, etc., to find the difference between two dates in months and days, respectively.

edit

Swift 3.0 and above

 func yearsBetweenDate(startDate: Date, endDate: Date) -> Int { let calendar = Calendar.current let components = calendar.dateComponents([.year], from: startDate, to: endDate) return components.year! } 
+11
source

In Swift 5 and iOS 12, you can use one of the 3 solutions below to calculate the difference (in years, months, days) between two dates.


# 1. Using Calendar dateComponents(_:from:to:) method

Calendar is the dateComponents(_:from:to:) method. dateComponents(_:from:to:) has the following declaration:

 func dateComponents(_ components: Set<Calendar.Component>, from start: DateComponents, to end: DateComponents) -> DateComponents 

Returns the difference between the two dates specified as DateComponents .

The Playground example below shows how to use dateComponents(_:from:to:) to calculate the difference between two dates:

 import Foundation let calendar = Calendar.current let startComponents = DateComponents(year: 2010, month: 11, day: 22) let endComponents = DateComponents(year: 2015, month: 5, day: 1) let dateComponents = calendar.dateComponents([.year, .month, .day], from: startComponents, to: endComponents) print(dateComponents) // prints: year: 4 month: 5 day: 9 isLeapMonth: false 

# 2. Using Calendar dateComponents(_:from:to:) method

Calendar is the dateComponents(_:from:to:) method. dateComponents(_:from:to:) has the following declaration:

 func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents 

Returns the difference between two dates.

The Playground example below shows how to use dateComponents(_:from:to:) to calculate the difference between two dates:

 import Foundation let calendar = Calendar.current let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))! let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))! let dateComponents = calendar.dateComponents([.year, .month, .day], from: startDate, to: endDate) print(dateComponents) // prints: year: 4 month: 5 day: 9 isLeapMonth: false 

# 3. Using string(from:to:) DateComponentsFormatter string(from:to:) method

DateComponentsFormatter has a method called string(from:to:) . string(from:to:) has the following declaration:

 func string(from startDate: Date, to endDate: Date) -> String? 

Returns a formatted string based on the time difference between two dates.

The Playground example below shows how to use string(from:to:) to calculate the difference between two dates:

 import Foundation let calendar = Calendar.current let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))! let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))! let formatter = DateComponentsFormatter() formatter.unitsStyle = .full formatter.allowedUnits = [.year, .month, .day] let string = formatter.string(from: startDate, to: endDate)! print(string) // prints: 4 years, 5 months, 9 days 
+2
source

Try this one

 func calculateDiffInTwoDate (date1: NSDate, date2: NSDate) -> NSInteger { //var userAge : NSInteger = 0 let calendar : NSCalendar = NSCalendar.currentCalendar() let unitFlags : NSCalendarUnit = [ .Year , .Month, .Day] let dateComponentNow : NSDateComponents = calendar.components(unitFlags, fromDate: date2) let dateComponentBirth : NSDateComponents = calendar.components(unitFlags, fromDate: date1) if ( (dateComponentNow.month < dateComponentBirth.month) || ((dateComponentNow.month == dateComponentBirth.month) && (dateComponentNow.day < dateComponentBirth.day)) ) { return dateComponentNow.year - dateComponentBirth.year - 1 } else { return dateComponentNow.year - dateComponentBirth.year } } 

Through this you can get the difference between the two dates in Years

0
source

Why don't you use the inbuild method to find the difference between two dates in seconds, and then write a method to convert seconds to years, months, and days.

 let diff = date1.timeIntervalSinceDate(date2) 
0
source
  //Assigning Dates let StartDate = datePicker.date let currentDate = Date() //Finding Difference of Dates let components = Set<Calendar.Component>([.day, .month, .year]) let differenceOfDate = Calendar.current.dateComponents(components, from: StartDate, to: currentDate) Print(differenceOfDate) 
0
source

Source: https://habr.com/ru/post/1236226/


All Articles