iPhone sdk - 从 NSFileCreationDate 获取日期

发布于 2024-12-03 15:57:08 字数 231 浏览 1 评论 0原文

我使用 attributesOfItemAtPath: 获取文件的创建日期和时间。我得到 NSString 作为结果,显示 创建日期:2011-08-23 10:47:33 +0000。但是,如何从 NSString 中单独检索有关日期的信息,而忽略有关时间的信息。即我的结果应该类似于创建日期:2011-08-23

I get the created date and time of a file using attributesOfItemAtPath:. I get the NSString as result which displays Date Created: 2011-08-23 10:47:33 +0000. But, how can I retrieve the information about the date alone from the NSString ignoring the information about time. i.e. my result should be like Date Created: 2011-08-23.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2024-12-10 15:57:08

attributeOfItemAtPath: 不返回 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];

Doesn't the attributesOfItemAtPath: return an NSDate? That's what the docs says. To get a string with just the date from an NSDate, use an NSDateFormatter. Code might look a bit like this:

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];
一曲爱恨情仇 2024-12-10 15:57:08

您实际上返回的是 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;

You'r actual getting back an NSDate not string:

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

With a NSDateFormatter you get the string from that date:

NSDateFormatter *formatter = [[NSDateFormatter alloc] 
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *datestring = [formatter stringFromDate:modifiedDate];
[formatter release], formatter = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文