NSDate 将格式设置为 MM/YYYY 但结果显示 dd/YYYY

NSDate set format to MM/YYYY but result shows dd/YYYY

我有一个示例代码如下,我想在 MM/yyyy 中展示。但是,我得到的结果是 dd/yyyy.

NSString *strJoinedDate = [prefs stringForKey:@"creation_date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss a"];

NSDate *JoinedDate = [dateFormatter dateFromString:strJoinedDate];
[dateFormatter setDateFormat:@"MM/yyyy"];
NSString *strFinalJoinedDate =[NSString stringWithFormat:@"Since %@",[dateFormatter stringFromDate:JoinedDate]];

日期字符串的日期格式有误,例如 5/29/2018 3:23:28 AM。变化:

dd/MM/yyyy HH:mm:ss a

至:

MM/dd/yyyy hh:mm:ss a

您还需要将日期格式化程序的语言环境设置为 en_US_POSIX

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