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.
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With