Convert NSDate to mm/dd/yyyy

Deadly 提交于 2019-12-19 05:18:56

问题


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.


回答1:


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]);


来源:https://stackoverflow.com/questions/8825043/convert-nsdate-to-mm-dd-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!