1

I have the following code:

var components = DateComponents()
components.year = 2017
components.month = 1

var calendar = Calendar.current
let date = calendar.date(from: components)!

let formatter = DateFormatter()
formatter.setLocalizedDateFormatFromTemplate("MMMMYYYY")
let string = formatter.string(from: date)

When I run this in the simulator on my computer it works as I would expect and the value of string is January 2017.

However, when I run the same code on my device the value of string is January 2016. What can be causing this difference?

Fortega
  • 19,463
  • 14
  • 75
  • 113
Daniel Wood
  • 4,487
  • 3
  • 38
  • 36

1 Answers1

1

The difference could be

  • A different locale on device and simulator.

and / or

  • YYYY is year in a Week of Year based calendar.
  • yyyy is year in standard calendar.

You should always use yyyy.

vadian
  • 274,689
  • 30
  • 353
  • 361