iPhone sdk-从NSFileCreationDate获取日期
我使用iPhone sdk-从NSFileCreationDate获取日期,iphone,ios4,nsdate,nsdateformatter,Iphone,Ios4,Nsdate,Nsdateformatter,我使用属性FiteMatPath:获取文件的创建日期和时间。我得到的结果是NSString,它显示创建日期:2011-08-23 10:47:33+0000。但是,如何仅从NSString中检索有关日期的信息,而忽略有关时间的信息。i、 e.我的结果应该是创建日期:2011-08-23您实际得到的是NSDate非字符串: NSString *filePathName = [directory stringByAppendingPathComponent:fileName]; NSDictiona
属性FiteMatPath:
获取文件的创建日期和时间。我得到的结果是NSString
,它显示创建日期:2011-08-23 10:47:33+0000
。但是,如何仅从NSString
中检索有关日期的信息,而忽略有关时间的信息。i、 e.我的结果应该是创建日期:2011-08-23
您实际得到的是NSDate
非字符串:
NSString *filePathName = [directory stringByAppendingPathComponent:fileName];
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePathName error:&error];
// No attributes then jst skip the file
if (!attributes || error) {
NSLog(@"Error getting attributes for: %@\nWith error: %@", filePathName, error);
retun;
}
NSDate *modifiedDate = [attributes objectForKey:NSFileModificationDate];
使用NSDateFormatter
可以从该日期获取字符串:
NSDateFormatter *formatter = [[NSDateFormatter alloc]
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *datestring = [formatter stringFromDate:modifiedDate];
[formatter release], formatter = nil;
您实际返回的是
NSDate
非字符串:
NSString *filePathName = [directory stringByAppendingPathComponent:fileName];
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePathName error:&error];
// No attributes then jst skip the file
if (!attributes || error) {
NSLog(@"Error getting attributes for: %@\nWith error: %@", filePathName, error);
retun;
}
NSDate *modifiedDate = [attributes objectForKey:NSFileModificationDate];
使用NSDateFormatter
可以从该日期获取字符串:
NSDateFormatter *formatter = [[NSDateFormatter alloc]
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *datestring = [formatter stringFromDate:modifiedDate];
[formatter release], formatter = nil;
属性FiteMatPath:是否返回NSDate?医生是这么说的。要从NSDate中获取仅包含日期的字符串,请使用NSDateFormatter。代码可能看起来有点像这样:
NSDictionary* attributesDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath: path error: NULL];
NSDate* date = [attributesDictionary objectForKey: NSFileCreationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString* justDateString = [dateFormatter stringFromDate: date];
[dateFormatter release];
属性FiteMatPath:是否返回NSDate?医生是这么说的。要从NSDate中获取仅包含日期的字符串,请使用NSDateFormatter。代码可能看起来有点像这样:
NSDictionary* attributesDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath: path error: NULL];
NSDate* date = [attributesDictionary objectForKey: NSFileCreationDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString* justDateString = [dateFormatter stringFromDate: date];
[dateFormatter release];