I have a number of German dates in the format of "10.Feb.2016", i.e. 2 digits for the day, 3 letters for the month, 4 digits for the year.
I tried to use the date format dd.MMM.yyyy
, but that fails for 3 German months:
- 10.Mär.2016
- 10.Jun.2016
- 10.Jul.2016
That's because that's according to the docs, MMM
actually stands for "date abbreviation", which does to guarantee or imply exactly three letters.
How can I parse a date with exactly three letters for the month that will work in any language? Is this even possible with NSDateFormatter?
The code below illustrates the problem. It prints:
OK 10.Jan.2016
OK 10.Feb.2016
FAILED parsing: 10.Mär.2016
OK 10.Apr.2016
OK 10.Mai.2016
FAILED parsing: 10.Jun.2016
FAILED parsing: 10.Jul.2016
OK 10.Aug.2016
OK 10.Sep.2016
OK 10.Okt.2016
OK 10.Nov.2016
OK 10.Dez.2016
Here's the code:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "dd.MMM.yyyy"
dateFormatter.locale = NSLocale(localeIdentifier: "de_DE")
let input = [
"10.Jan.2016",
"10.Feb.2016",
"10.Mär.2016",
"10.Apr.2016",
"10.Mai.2016",
"10.Jun.2016",
"10.Jul.2016",
"10.Aug.2016",
"10.Sep.2016",
"10.Okt.2016",
"10.Nov.2016",
"10.Dez.2016",
]
for test in input
{
if let date = dateFormatter.dateFromString(test)
{
print("OK \(test)")
}
else
{
print("FAILED parsing: \(test)")
}
}