2011-06-09 87 views
1

Helo, 我正在尝试一堆代码,它给null。这段代码有什么问题吗?NSDateFormatter @“mmddyyy”?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    // get NSDate from old string format 
    [dateFormatter setDateFormat:@"mmddyyy"]; 
    NSDate *date = [dateFormatter dateFromString:@"02012002"]; 
    NSLog(@"%@",date); 
    // get string in new date format 
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"]; 
    NSString *strMyDate= [dateFormatter stringFromDate:date]; 
    NSLog(@"%@\n\n********************************************************",strMyDate); 

输出

2011-06-09 12:49:17.957 TestPrj[3054:207] (null) 
2011-06-09 12:49:17.959 TestPrj[3054:207] (null) 

******************************************************** 

回答

1

您④短缺的一个 'Y' 有排在第二线 - 它要么应该是 -

dateFormatter setDateFormat:@ “MMDDYYYY”];

或dateFormatter setDateFormat:@“MMddyyyy”];

+0

thnaks点 – 2011-06-09 07:56:03

2

请尝试

[dateFormatter setDateFormat:@"MMddyyyy"]; 

编辑

在我以前的版本DD没有工作在这个例子中,虽然会产生错误的结果开始与februar y - >回到dd

使用MM其他月份它会将其解析为分钟。请参考Unicode Technical Standard #35/Locale Data Markup Language (LDML)

+0

谢谢...为了快速回复...你能告诉我为什么是这样吗? – 2011-06-09 07:28:11

+0

@Inder Kumar Rathore在帖子中解释。 – 2011-06-09 07:50:21