我有以下方法,我想用它来返回修改后的日期:
- (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-02-06 04:44:57 +0000
创建格式化日期:(空)