iphone NSDate转换问题 [英] iphone NSDate Conversion problem

查看:119
本文介绍了iphone NSDate转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的图表Api for facebook ..

In my graph Api for facebook..

我收到这些数据..
来自Json ..

updated_time=2011-05-17T14:52:16 + 0000;

I am getting this data.. from Json..
"updated_time" = "2011-05-17T14:52:16+0000";

我正在使用此代码将其转换为有效的日期格式

and I am using this code to convert it into valid date format

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//2010-12-01T21:35:43+0000  
[df setDateFormat:@"yyyy-mm-ddHH:mm:ssZZZZ"];
NSDate *date = [df dateFromString:[[forTable valueForKey:@"updated_time"] stringByReplacingOccurrencesOfString:@"T" withString:@""]];
[df setDateFormat:@"eee MMM dd, yyyy hh:mm"];
pastDate = [df stringFromDate:date];
NSLog(@"and the date string is %@",pastDate);

它在控制台给我这个结果

and it is giving me this result in the console

,日期字符串是Mon 01 17,2011 08:22

虽然json值正在给出我是May的日期。这段代码给了我1月的日期

though json value is giving me the date of May.this code is giving me the date January

谁能告诉我这里发生了什么?

can anyone tell me what is happening here?

推荐答案

看起来您的日期格式字符串没有考虑字母T - 您可能应该在撇号之间附加文字文本,只是为了安全起见。尝试:

It looks like your date format string isn't taking the letter "T" into account – and you should probably be enclosing literal text between apostrophes, just to be safe. Try:

[df setDateFormat:@yyyy' - 'MM' - 'dd'T'HH':'mm':'ssZZZZ ];

这篇关于iphone NSDate转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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