如何将XML日期格式转换为Cocoa

时间:2009-08-10 23:22:57

标签: nsdate nsdateformatter

我有一个日期格式: 2009-08-10T16:03:03Z

我要转换为: @“MMM dd,HH:mm a”

我在NSString中检索xml格式。我试图使用NSDateformatter: NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@“MMM dd,HH:mm a”];

任何想法?

2 个答案:

答案 0 :(得分:6)

NSString *yourXMLDate = @"2009-08-10T16:03:03Z" 
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];

希望这有帮助!

答案 1 :(得分:2)

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
    NSDate *date = [dateFormatter dateFromString:self.currentDate];

    [item setObject:date forKey:@"date"];

    [items addObject:[item copy]];

此处编辑以下代码

[dateFormatter setDateFormat:@"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700

到您所需的格式。