格式化 NSDate

发布于 2024-08-11 17:22:00 字数 477 浏览 3 评论 0原文

我正在从 RSS 提要中提取数据。 feed 中的键之一是表示项目创建日期和时间的字符串。

我正在尝试将此字符串值转换为 NSDate。从 RSS 提要返回的字符串值如下: 2009-11-18T22:08:00+00:00

我尝试了以下代码但无济于事:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyyMMdd HH:mm"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

理想情况下;除了将该值转换为 NSDate 值之外,我还想使用手机上的本地化日期格式对其进行格式化。

任何指示都会有很大的帮助。

亲切的问候

I am pulling data from an RSS Feed. One of the keys in the feed is is a string representing the date and time the item was created.

I am trying to convert this string value to an NSDate. The string value is returned from the RSS feed as: 2009-11-18T22:08:00+00:00

I tried the following code to no avail:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyyMMdd HH:mm"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

Ideally; on top of converting the value to a NSDate value, I would also like to format it using the localised date format on the handset.

Any pointers would be a great help.

Kind Regards

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

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

发布评论

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

评论(2

就此别过 2024-08-18 17:22:00

检索 dateFormat:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

如果您想根据用户的区域设置设置时间和日期的格式,则可以使用预定义的格式:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"%@",[dateFormatter stringFromDate:someDate]);
[dateFormatter release]; // don't forget to release the dateformatter    

查看文档以了解哪种格式化程序最适合您。

To retrieve the dateFormat:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

You can use the predefined formats if you would like to format time and date according to the user's locale:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"%@",[dateFormatter stringFromDate:someDate]);
[dateFormatter release]; // don't forget to release the dateformatter    

Check the documentation to see which formatter suits you best.

漫漫岁月 2024-08-18 17:22:00

实际上 RSS feed 中的日期形式可能有所不同,因此解析它的最佳方法如下

[NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822];

这不是标准函数,可以在此处找到它的库 https://gist.github.com/953664

Actually date form in RSS feed may differ, so best way to parse it is following

[NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822];

This is not a standard function, library for it can be found here https://gist.github.com/953664

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文