how to convert datetime format in NSString?

柔情痞子 提交于 2019-12-03 20:27:36

问题


I have got value from the xml 2009-11-23T05:24:41.000Z.

Now i want to display string like this: Wed, Nov 4, 2009 8:00 PM - Wed, Nov 4, 2009 9:00 PM

How it possible?


回答1:


You'll want to do something like this:

NSDateFormatter * inputFormatter = [ [ [ NSDateFormatter alloc ] init ] autorelease ];
NSDateFormatter * outputFormatter = [ [ [ NSDateFormatter alloc ] init ] autorelease ];
[ inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.000Z'" ];
[ outputFormatter setDateFormat:@"EEE, MMM d, yyyy h:mm a" ]; 

NSString * inputString = @"2009-11-23T05:24:41.000Z";
NSDate * inputDate = [ inputFormatter dateFromString:inputString ];
NSString * outputString = [ outputFormatter stringFromDate:inputDate ];

// outputString = @"Mon, Nov 23, 2009 05:24 AM"

See the Unicode date format patterns guide for more information on how to configure NSDateFormatter.




回答2:


Use NSDateFormatter's methods -dateFromString: and -stringFromDate:.



来源:https://stackoverflow.com/questions/2076417/how-to-convert-datetime-format-in-nsstring

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