Difference between 2 dates in weeks and days using swift 3 and xcode 8

You can use Calendar's dateComponents(_:from:to:) to find the difference between 2 dates to your desired units.

Example:

let dateRangeStart = Date()
let dateRangeEnd = Date().addingTimeInterval(12345678)
let components = Calendar.current.dateComponents([.weekOfYear, .month], from: dateRangeStart, to: dateRangeEnd)

print(dateRangeStart)
print(dateRangeEnd)
print("difference is \(components.month ?? 0) months and \(components.weekOfYear ?? 0) weeks")


> 2017-02-17 10:05:19 +0000
> 2017-07-10 07:26:37 +0000
> difference is 4 months and 3 weeks

let months = components.month ?? 0
let weeks = components.weekOfYear ?? 0

You are close. Just add .weekOfMonth (meaning "quantity of weeks" according to the API documentation) to the allowed units. Example:

let now = Date()
let endDate = now.addingTimeInterval(24 * 3600 * 17)

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .weekOfMonth]
formatter.unitsStyle = .full
let string = formatter.string(from: now, to: endDate)!

print(string) // 2 weeks, 3 days

Setting maximumUnitCount = 2 is not necessary because there are only two allowed units.