1

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: enter image description here

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)
schmidiii
  • 316
  • 2
  • 13
  • 2
    `YYYY` (meaning (Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.) vs `yyyy`? – Larme May 10 '17 at 08:26
  • Holy! how could i miss this. especially since i work with Year for Week of Year as well. Do you mind posting this as answer so i can accept? – schmidiii May 10 '17 at 14:42

0 Answers0