Ios NSDateFormatter未创建数据

Ios NSDateFormatter未创建数据,ios,objective-c,cocoa-touch,nsdateformatter,Ios,Objective C,Cocoa Touch,Nsdateformatter,这种方法有什么不对?返回值为空 -(NSString *)newFormatDateStringFromOldFormatStringForTableView:(NSString *)oldString { // Old String "2013-11-29 15:00:00" // New Required Stirng 'Fri, 01 Dec 2013' // Convert string to date object NSDateFormatter *dateFormat = [[NS

这种方法有什么不对?返回值为空

-(NSString *)newFormatDateStringFromOldFormatStringForTableView:(NSString *)oldString
{
// Old String "2013-11-29 15:00:00"
// New Required Stirng 'Fri, 01 Dec 2013'

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setDateFormat:@"EEE, dd MMM yyyy"];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:oldString];  

// Convert date object to desired output format
//[dateFormat setDateFormat:@"EEEE, MMMM d, YYYY"];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy"];
NSString *newString = [dateFormat stringFromDate:date];  
[dateFormat release];   

return newString;
}

谢谢你的帮助

您必须像这样告诉旧字符串的完整格式

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
以下代码正常工作:)


}

对象“date”没有以某种方式创建,它为零。@Zubair它应该工作。它应该工作,但不知道为什么它不工作。如果它不工作,可能oldString不是你认为的那样。尝试将NSLog作为函数中的第一件事来输出oldString。这里有数千个如何使用NSDateFormatter的示例。看看其中的一些。如果它有效,为什么不从一开始就使用它呢?@HotLicks,我正在寻找有关格式说明符的详细信息,只是看到了它,所以我尝试:)。。这种方法有什么问题吗?没有什么问题,随便说说。对你来说可能有点过分。@HotLicks,嗯。。谢谢:)如果此解决方案有效,那么为什么您最初声明日期字符串的格式不同?
+ (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);
    [userVisibleDateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}else{
    userVisibleDateTimeString=rfc3339DateTimeString;
}
return userVisibleDateTimeString;