NSDateFormatter is adding AM/PM when date format doesn't specify it

Viewed 118

I have simple code that transforms a date into a string:

+ (NSString *)dateStringFromDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    return [dateFormatter stringFromDate:date];
}

I would expect a string like:

2020-10-19T11:55:29Z

But instead it is outputting a string like this:

2020-10-19T12:38:06 p.m.Z

Note the "p.m." in the output. I am not using the "a" date format character, so I would not expect to see AM or PM.

When I run the code in the simulator, it produces the correct result. When I run it on a physical iPad running iPadOS 14.0.1 it produces the incorrect result.

I have noticed that forcing the locale with this fixes the issue:

dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];

But, I'm not sure if that is the correct approach, as this seems like a bug.

Can anyone shed light on why this is happening?

0 Answers
Related