我有以下的方法,我想用回修改日期:格式化的NSDate不工作
- (NSDate *)getCreationDate:(NSFileManager *)fileManager atPath:(NSString *)path {
NSError *error;
NSDate *date;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:&error];
// Get creation date.
if (!error) {
if (fileAttributes != nil) {
NSDate *creationDate = [fileAttributes fileCreationDate];
NSString *dateString = [creationDate description];
NSLog(@"Unformatted Date Created: %@", dateString);
// Format date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss"];
date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(@"Formatted Date Created: %@", [date description]);
} else {
NSLog(@"File attributes not found.");
}
} else {
NSLog(@"%@", [error localizedDescription]);
}
return date;
}
的问题是格式化的日期是回来为空。
输出:
创建无格式的日期:2013年2月6日4时44分57秒+0000
格式化创建日期:(空)
您需要查看文档。 'description'没有定义的返回格式;你试图依靠无证行为。我建议你再读一遍'NSDate'是什么;你似乎已经发明了一种观念,即它没有时就有格式。这只是一个特定时刻的不透明表示。 – Tommy
你的代码有太多错误,很难知道从哪里开始。 –