开发者

how to convert datetime format in NSString?

开发者 https://www.devze.com 2022-12-17 01:43 出处:网络
I have got value from the xml 2009-11-23T05:24:开发者_Go百科41.000Z. Now i want to display string like this: Wed, Nov 4, 2009 8:00 PM - Wed, Nov 4, 2009 9:00 PM

I have got value from the xml 2009-11-23T05:24:开发者_Go百科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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消