iphone NSDate Conversion problem

女生的网名这么多〃 提交于 2019-12-03 13:54:52

问题


In my graph Api for facebook..

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

and the date string is Mon 01 17, 2011 08:22

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?


回答1:


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




回答2:


The dateFormat was wrong, it should be with capital Ms for the month:

[df setDateFormat:@"yyyy-MM-ddTHH:mm:ssZZZZ"];


来源:https://stackoverflow.com/questions/6068387/iphone-nsdate-conversion-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!