NSDateFormatter不适用于Twitter日期 [英] NSDateFormatter not Working for Twitter date

查看:111
本文介绍了NSDateFormatter不适用于Twitter日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用STTwitter库格式化从Twitter获得的日期.

I am trying to format a date i am getting from twitter using the STTwitter library.

但是到目前为止,我尝试过的代码仍然无法正常工作.

However the code that I've tried so far has not worked.

从Twitter获取日期的代码:

Code for getting the date from twitter:

NSString *dateString = [status valueForKey:@"created_at"];

这将返回制作鸣叫的时间,日期,时区和年份,看起来很杂乱.

This returns the time, date, time zone and year in which the tweet was made which looks messy.

我尝试使用以下代码将其转换并变得更整洁:

I tried using the following code to convert this and make it neater:

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MMddHHmm"];

NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"%@", dateFromString);


dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd MMMM' at 'hhmm a"];
NSString *mydate=[dateFormatter stringFromDate:dateFromString];

然后尝试将结果放在文本标签中:

And then try to put the result in a text label:

cell.detailTextLabel.text = my date;

我已经尝试了日期格式化程序的许多不同变体,但都没有奏效,我也不知道为什么.

Ive tried many different variations of the Date Formatter but none have worked and i have no idea why.

感谢您的帮助:)

推荐答案

您使用的日期格式甚至没有关闭结果中使用的日期字符串,类似于Fri Nov 18 20:35:49 +0000 2011.

The date format you are using is not even close the date string used in the result, which is something like Fri Nov 18 20:35:49 +0000 2011.

NSString *dateStr = @"Fri Nov 18 20:35:49 +0000 2011";

NSDateFormatter *dateFormatter= [NSDateFormatter new];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];

NSDate *date = [dateFormatter dateFromString:dateStr];

真正的把戏是在使用的语言环境中,因为日期以英语进行了本地化.

The real trick is in the locale used, since the date is localized in english.

这篇关于NSDateFormatter不适用于Twitter日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆