Filed under: iOS
Consider this snippet from a playground
let df = DateFormatter() df.dateFormat = "YYYY-MM-dd" df.string(from: Date(year: 2017, month: 12, day:30)!)
The result is
"2017-12-30" as you might expect.
But, try the next day:
The reason is because
YYYY is not what you think it is. The correct way to format the year part of a date is with
yyyy. There’s a good answer on StackOverflow that explains why, but the short version is that
YYYY is part of a formatting system based on week of the year, which always starts on a Sunday (EDIT: in the US, that is. Other locales’ weeks start on different days, so the exact dates that will be wrong vary by locale — thanks to dc6k and others on r/iOSProgramming for the correction)
December 31st, 2017 is the first day of the first week of 2018. If you are using the week-of-year formatting system, then
YYYY is what you do to get the year part out. On other years, you might find that the first week of January has a
YYYY of the previous calendar year.
So, stop what you’re doing right now and search your codebase for
YYYY and fix it if you find any. You have a week or so to submit before Apple closes the App Store for the holidays.
Oh yeah, this isn’t just an iOS issue. The format string that
DateFormat uses is part of a standard that a lot of languages/frameworks use, so check your code no matter what it’s written in (and check your docs, of course). iOS devs do have a limited time window though because of review times and the App Store holiday.