将NSDate转换为mm / dd / yyyy [英] Convert NSDate to mm/dd/yyyy

查看:93
本文介绍了将NSDate转换为mm / dd / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题与其他问题类似,但我无法找到我的代码出错的地方。我想将当前日期转换为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]);

问题是月份。每次运行此代码时,月份都不同。例如,我的第一次日期是32/11/2012,我刚刚运行它,它是55/11/2012。

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.

谢谢。

推荐答案

检查案例:

mm 表示分钟

MM 表示月数

因此您的代码可能应该读取

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

这篇关于将NSDate转换为mm / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆