iOS5数据格式化程序?

iOS5数据格式化程序?,ios5,nsdateformatter,Ios5,Nsdateformatter,我对NSDateFormatter和iOS有问题。事实上,我在这里找到了对问题的认识和解释: 然而,我的大脑无法处理这些信息,无法将这些信息转化为针对我特定情况的解决方案。我希望你们能在这里帮助我: 谁能告诉我如何在iOS5中完成以下工作?它过去工作得很好: NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_GB”]; [inputFormatter setLocale: myLocale]; [myLoc

我对NSDateFormatter和iOS有问题。事实上,我在这里找到了对问题的认识和解释:

然而,我的大脑无法处理这些信息,无法将这些信息转化为针对我特定情况的解决方案。我希望你们能在这里帮助我:

谁能告诉我如何在iOS5中完成以下工作?它过去工作得很好:

NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_GB”];
[inputFormatter setLocale: myLocale];
[myLocale release];
[inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *formattedDate = [inputFormatter dateFromString: feedDateString];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEEE d MMMM"];
我无法控制的输入字符串类似于“Tue,2012年1月3日00:00:00+0100” 当我记录[outputFormatter stringFromDate:formattedDate]时,我得到空值

最初的地点是“en_US”,但我改变了,希望这会有所帮助

这也将有助于这个职位上的人们,这似乎是一个死胡同:

试试这个:

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]];
/* 
   Do something with this string
*/
[outputFormatter release];
您还可以通过向NSDateFormatter添加setLocale参数来添加区域

NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
[outputFormatter setLocale: myLocale];
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]];

/* 
   Do something with this string
*/

[outputFormatter release];
[myLocale release];

感谢你们所有在这件事上花费脑力的人:这都是我愚蠢的过错: 需要格式化的输入字符串不是“Mon,2012年2月13日00:00:00+0100”,而是“Mon,2012年2月13日00:00:00+0100” " 看到了吗?嗯,我在NSLogging期间没有看到尾随的空格。[讽刺的是:字符串后面的许多空格和return也没有显示在这里!]

不知何故,iOS4并不在意它,iOS5只是更加挑剔,而且可能是正确的。 所以我解决了这个问题如下:

NSString *trimmedString = [inputstring stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];

我希望这也能对您有所帮助。

谢谢,不幸的是,它并不能解决我的问题。在您的示例中,您使用[NSDate date]。使用该值可以正常工作。因此,显然,我必须检查我的inputFormatter。