我有以下的方法,我想用回修改日期:格式化的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
你的代碼有太多錯誤,很難知道從哪裏開始。 –