如何在NSString中转换日期时间格式?

时间:2010-01-16 06:26:06

标签: iphone objective-c nsdate nsdateformatter

我从xml 2009-11-23T05:24:41.000Z获得了价值。

现在我想显示如下字符串:Wed, Nov 4, 2009 8:00 PM - Wed, Nov 4, 2009 9:00 PM

怎么可能?

2 个答案:

答案 0 :(得分:3)

使用NSDateFormatter的方法-dateFromString:-stringFromDate:

答案 1 :(得分:2)

你会想做这样的事情:

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"

有关如何配置NSDateFormatter的详细信息,请参阅Unicode date format patterns guide