2

这一直让我发疯,有人请告诉我我在哪里搞砸了。

我也创建了一个新项目来测试它。环境是在模拟器中运行的 xcode 3.2.5,并且在设备上具有相同的结果。

当我创建日期格式化程序并设置其格式然后传入字符串时,日期格式化程序无法正常工作。

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

这将导致以下输出:

[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

我不确定为什么在我给它格式 MMMM 时它会打印一月,因为这在我的应用程序的其他部分工作得很好,它只是使用不同的 NSDate 源。

4

2 回答 2

3

您使用 'm' 作为月份编号,用于解释和生成字符串。不幸的是,'m' 是分钟。请改用“M”。

于 2010-12-04T00:29:12.563 回答
1

如果将 tmpMonth 设置为 12,输出是什么?我想知道它会留在一月还是切换到二月?

于 2010-12-04T00:39:38.833 回答