NSDateFormatter不适用于Twitter日期

编程入门 行业动态 更新时间:2024-10-08 06:27:24
本文介绍了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日期

本文发布于:2023-11-27 11:08:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637822.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   日期   NSDateFormatter   Twitter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!