Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone sdk-从NSFileCreationDate获取日期_Iphone_Ios4_Nsdate_Nsdateformatter - Fatal编程技术网

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];