2015-02-10 87 views
2

我试图从NSUserDefaults格式化一个字符串,其中包含一个日期。 结果是(null)。 我的问题是:有没有办法将此字符串转换为格式化日期,或者有更好的方法吗?从NSUserdefaults转换/格式DateString

我的代码:

NSString *c = [prefs objectForKey:@"expiration_date_full"]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm:ss"];    
NSDate *dateFromString = [[NSDate alloc] init];    
dateFromString = [dateFormatter dateFromString:c];   

cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@ Noch gültig bis: %@", a, b, dateFromString]; 
+0

你有没有在C字符串值? – karthikeyan 2015-02-10 10:10:27

+0

是的,在c中有字符串像2014-22-11 18:06:35 +0000 – 2015-02-10 10:51:01

回答

1

可以在存储NSUserDefaults的一个NSDate无需转换。也就是说,NSDate是PLIST支持的类型之一。您可以查看documentation了解更多信息。

1

更改日期格式

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"MM-dd-yyyy"]; 
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
    NSDate* myTime = [dateFormat dateFromString:@"07-22-2014"]; 
    NSLog(@"%@",myTime); 
+0

仍然返回null。 – 2015-02-10 11:03:02