开发者

NSDateFromatter not giving the proper MMMM value

开发者 https://www.devze.com 2023-01-29 06:58 出处:网络
this has been driving me crazy, someone ple开发者_StackOverflow中文版ase show me where i screwed it up.

this has been driving me crazy, someone ple开发者_StackOverflow中文版ase show me where i screwed it up.

I have created a new project to test this as well. Environment is xcode 3.2.5 running in the simulator and on a device has same result.

When i create a date formatter, and set its format then pass in a string, something is not working on the date formatter.

int tmpMonth = 11;
int tmpYear = 2010;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setDateFormat:@"m/d/yyyy"];
NSString *dateAsString = [[NSString alloc] initWithFormat:@"%d/23/%d",tmpMonth,tmpYear];
NSDate *theDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:[dateFormatter dateFromString:dateAsString]];
[dateFormatter setDateFormat:@"MMMM d, yyyy"];
NSLog(@"%@",[dateFormatter stringFromDate:theDate]);
[dateFormatter setDateFormat:@"m/d/yyyy"];
NSLog(@"%@",[dateFormatter stringFromDate:theDate]);
[dateFormatter release];
[theDate release];
[dateAsString release];

this results in this output:

[Session started at 2010-12-03 17:47:04 -0600.]
2010-12-03 17:47:05.274 wtfDateFormatter[18970:207] January 23, 2010
2010-12-03 17:47:05.277 wtfDateFormatter[18970:207] 11/23/2010

I am not sure why it is printing January when I give it the format MMMM, as this is working just fine in other parts of my app, it just uses a different NSDate source.


You're using 'm' for month number, both for interpreting and generating strings. Unfortunately, 'm' is for minutes. Use 'M' instead.


Whats the output if you set the tmpMonth to 12? I wonder if it would stay January or switch to February?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号