Maybe the question is a bit miss leading. DateInterval does work but only if i use Styles not if i use a custom Format.
I Made a Playground to illustrate. Notice how the year part of the first date changes from 2015 to 2016:
Correct value while using Formatter.dateStyle:
"Dec 27, 2015 - Jan 3, 2016"
Wrong value when using Formatter.dateTemplate:
"Dec 27, 2016 - Jan 3, 2016"
Is this a bug with Apple or am i missing something?
Here is the playground code for reference:
import UIKit
import Foundation
let intervalFormatter = DateIntervalFormatter()
let format = DateFormatter.dateFormat(fromTemplate: "YYYYMMMd",
options: 0,
locale: intervalFormatter.locale)
intervalFormatter.dateStyle = DateIntervalFormatter.Style.medium
intervalFormatter.timeStyle = DateIntervalFormatter.Style.none
let fromInvalid = Date(timeIntervalSinceReferenceDate: TimeInterval(472950000.0))
let toInvalid = Date(timeIntervalSinceReferenceDate: TimeInterval(473468400.0))
intervalFormatter.string(from: fromInvalid, to: toInvalid)
intervalFormatter.dateTemplate = format
intervalFormatter.string(from: fromInvalid, to: toInvalid)
let fromValid = Date(timeIntervalSinceReferenceDate: TimeInterval(504399600.0))
let toValid = Date(timeIntervalSinceReferenceDate: TimeInterval(504918000.0))
intervalFormatter.string(from: fromValid, to: toValid)