2014-01-15 34 views
0

我有一個來自服務器的日期格式爲2013-12-30T14:59:00. + 0000我想將其轉換爲MMM格式DD,YYYY H:毫米日期格式從2013-12-30T14:59:00更改+ 0000

Im做如下

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init]; 
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 
[dateFormatter setTimeZone:timeZone]; 
NSDate *dateFromString = [dateFormatter dateFromString:selectedDate]; 
if (dateFromString != nil) 
{ 
     [dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"]; 
     stringDateFromDate = [dateFormatter stringFromDate:dateFromString]; 
} 

但 「dateFromString」 來作爲零。

這裏可以有任何幫助嗎

回答

0

在我看來,現在已經太遲了。但我決定寫。

參見本例中爲您selectedDate字符串,這樣做:

NSString *selectedDate = @"2013-12-30T14:59:00.+0000"; 

// I donno what "T" means, that's just replace it to an empty string 
selectedDate = [selectedDate stringByReplacingOccurrencesOfString:@"T" withString:@" "]; 

// get string without .+0000 
NSRange range = [selectedDate rangeOfString:@"."]; 
selectedDate = [selectedDate substringToIndex:range.location]; 

NSString *stringDateFromDate; 

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init]; 
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; 
[dateFormatter setTimeZone:timeZone]; 

// necessarily set this date format! 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *dateFromString = [dateFormatter dateFromString:selectedDate]; 

if (dateFromString != nil) 
{ 
[dateFormatter setDateFormat:@"MMM dd,yyyy h:mm a"]; 
stringDateFromDate = [dateFormatter stringFromDate:dateFromString]; 
} 

// That's all. Output your string. 
NSLog(@"%@", stringDateFromDate); 
相關問題