English 中文(简体)
NSDateFormater从NSMutableArray值中获取
原标题:NSDateFormater from NSMutableArray value

I store some dates in my NSMutableArray, I want to retrieve them and change the format. For the moment I do this:


NSDate *myDate = [self.dates objectAtIndex:0];
NSLog(@"myDate:%@", myDate); // 2010-03-02
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:@"MMMM d YYYY"];

NSString *newDate = [formatDate stringFromDate:myDate];
NSLog(@"newDate: %@", newDate); // NULL

newDate的结果是NULL,我希望它变成像这样的东西:“2010年3月3日”。

谢谢 (xiè xiè)

最佳回答

由于您的日期数组包含字符串,因此您必须将字符串转换为NSDate,然后可以将日期重新转换为所需格式。

NSDateFormatter *strToDateFmt = [[NSDateFormatter alloc] init];
[strToDateFmt setDateFormat:@"yyyy-MM-dd"];
NSDate *myDate = [strToDateFmt dateFromString:[self.dates objectAtIndex:0]];
[strToDateFmt release];
NSLog(@"myDate:%@", myDate);

NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:@"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
[formatDate release];
NSLog(@"newDate: %@", newDate);
问题回答

对我有用,但我用第一行替换了它:

NSDate *myDate = [NSDate date];

我的输出:

2010-03-02 19:29:08.045 app[11464:a0f] myDate:2010-03-02 19:29:08 -0800
2010-03-02 19:29:08.048 app[11464:a0f] newDate: March 2 2010

我的最佳猜测是 [self.dates objectAtIndex:0] 并没有返回你想要的东西。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签