2

this has been driving me crazy, someone please 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.

AtomRiot
  • 1,869
  • 3
  • 18
  • 24

2 Answers2

3

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

James Huddleston
  • 8,410
  • 5
  • 34
  • 39
1

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

dmcgill50
  • 528
  • 6
  • 26