Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSDate to mm/dd/yyyy

I know this question is similar to others asked, but I can't find where my code is going wrong. I am trying to convert the current date into a mm/dd/yyyy format. Here is what I am using:

NSDate *date = [[NSDate alloc]init];

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"mm/dd/yyyy"];

NSLog(@"%@", [formatter stringFromDate:date]);

The problem is the month. Every time I run this code, the month is different. For example the first time my date was 32/11/2012, and I just ran it and it was 55/11/2012.

Is there a reason why this would keep changing? The days and years appear to be fine.

Thanks.

like image 741
coder Avatar asked Jan 11 '12 18:01

coder


1 Answers

Check the case:

mm means minutes

MM means months

So your code should probably read

NSDate *date = [[NSDate alloc]init];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];

NSLog(@"%@", [formatter stringFromDate:date]);
like image 125
PengOne Avatar answered Nov 16 '22 23:11

PengOne